I have two commands that need to be executed in PowerShell. Everything else can be down in Batch. I could write the whole thing in PowerShell, but I'd have to make sure the execution policy is set correctly, and that could be a security issue for our netadmins on these servers.
In my Batch script, I need to:
- Bring down the IIS application cache (I can use AppCmd)
- Download a new zip f开发者_运维知识库ile from the server (using my wget version of Powershell)
- Delete all the old files in the www directory
- Unzip the zip file in the www directory
- Bring up the IIS application cache.
In Step #2, my two powershell commands are:
$client = new-object System.Net.WebClient
$client.DownloadFile("http://path/to/file", "file.name")
Is there a way I can combine these two lines into a single Powershell command, much like the way I can in Unix using semicolons? I've tried to put a semicolon between the two lines and get the following error:
Missing ')' in method call.
At line:1 char:62
+ $client.DownloadFile( <<<< http://path/to/file )
+ CategoryInfo : ParserError: (CloseParenToken:TokenId) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
However, inside an actual Powershell script called "wget.ps1", it executes with this:
$ powershell -c wget.ps1
Well you have at least two options. The first is to define the execution policy when you run the script. This only changes the policy for the one session that you are kicking off:
powershell.exe -File 'C:\Path\To\File.ps1' -ExecutionPolicy RemoteSigned
Or you can just pass the commands straight in:
powershell.exe -command {$string = 'bobdee';$client.DownloadFile("http://path/to/file", "file.name")}
You can do this:
(new-object System.Net.WebClient).DownloadFile("http://path/to/file", "file.name")
2013 Update:
I'm not sure how things were back in '11, but these days you can do the following in PowerShell:
wget "http://path/to/file" -outfile "file.name"
精彩评论