2013-08-26 115 views
22

我正在尋找一種方法來從命令提示符運行幾個PowerShell命令。我不想爲此創建腳本,因爲它只是一些我需要運行的命令,因爲我不知道如何使用PowerShell編寫腳本。從命令提示符運行PowerShell命令(無ps1腳本)

這裏是我想用來啓動用命令:

Get-AppLockerFileInformation -Directory <folderpath> -Recurse -FileType <type> 

我真的不希望爲此創建腳本,如果我可以只運行一個或者它會更容易兩個命令從一個批處理文件與其餘的東西。編輯: 這是我到目前爲止嘗試過的。

1)

powershell -Command "Get-AppLockerFileInformation....." 
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program.... 

2)

powershell -Command {Get-AppLockerFileInformation.....} 

用這種方式沒有錯誤,但我沒有得到任何回報。如果我使用Set-AppLockerPolicy...什麼也沒有發生。

3)

powershell -Command "{Get-AppLockerFileInformation.....}" 
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program.... 

4)

powershell -Command "& {Get-AppLockerFileInformation.....}" 
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program.... 

5)

powershell "& {Get-AppLockerFileInformation.....}" 
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program.... 

6)

powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -Command {Get-AppLockerFileInformation....} 

沒有錯誤,但沒有發生。

7)

powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -Command "Get-AppLockerFileInformation...." 

沒有錯誤,但沒有任何反應。

+1

一方面你說「我真的不想創建一個腳本「,另一方面你說」如果我只能從批處理文件運行一個或兩個命令「。所以:這是它? – djangofan

+0

我應該澄清,我不想創建一個PowerShell腳本。 –

+0

當從新的乾淨的PowerShell提示運行時,Get-AppLockerFileInformation對你來說工作正常嗎?什麼OS? (在Win7中測試Get-App ..並且在這種情況下沒有這樣的cmdlet,因此得到相同的錯誤消息) – NiKiZe

回答

38

這裏是只能回答​​是設法爲我的問題的工作,在網頁this的幫助下找到了它(很好的參考)。

powershell -command "& {&'some-command' someParam}" 

而且,這裏是一個整潔的方式做多個命令:

powershell -command "& {&'some-command' someParam}"; "& {&'some-command' -SpecificArg someParam}" 

例如,我這是怎麼跑到我的兩個命令:

powershell -command "& {&'Import-Module' AppLocker}"; "& {&'Set-AppLockerPolicy' -XmlPolicy myXmlFilePath.xml}" 
+1

下面是另一個有用的技巧,你可以從命令提示符運行一個ps1腳本,並像下面這樣傳遞參數:'powershell。 exe文件「C:\ myfile.ps1」arg1 arg2 arg3'或'powershell.exe -File「C:\ myfile.ps1」-Some-Command arg1 -Another-Command arg2' –

+2

命令'powershell -command「& {&'導入模塊'AppLocker}「; 「&{&Set-AppLockerPolicy'-XmlPolicy myXmlFilePath.xml}」'在單獨的PS會話中運行這2個PS命令,即它不適用如果您需要在2個命令之間共享某個變量。解決方法如下:'powershell -command「&{$ someVar ='test';&'Write-Host'$ someVar}」' –

+0

@Maciej我不認爲它運行在不同的PS會話,否則第二部分這個特定的命令會失敗,因爲我需要從命令的第一部分加載該模塊。我不同意。但是,正如你所提到的,如果需要的話,共享變量可能有一定的價值。 –

1

也許powershell -Command "Get-AppLockerFileInformation....."

看看powershell /?

+0

這就是我一直在嘗試,但我似乎並沒有得到語法非常正確或什麼的。當我直接從powershell運行命令時,它工作正常,但... –

+0

好吧,你有任何錯誤或其他什麼?請編輯您的問題並提供更多信息。你有什麼嘗試,什麼不工作等。 – NiKiZe

+0

請參閱我上面的編輯。 –

6

運行一個單一命令行上像這樣:

powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile 
    -WindowStyle Hidden -Command "Get-AppLockerFileInformation -Directory <folderpath> 
    -Recurse -FileType <type>" 
+0

只要我有機會,我會嘗試一下。這裏似乎有很多標誌,如果我想使用Set-AppLockerPolicy和New-AppLockerPolicy,它會是一樣的嗎? –

+0

這沒有奏效,更不用說-WindowStyle標誌關閉我的命令提示符了......此外,我不需要ExecutionPolicy標誌,因爲我直接使用命令行。 –

+0

是的,有些選項可能是不必要的。只需調整,取決於你需要什麼。這裏有更多的信息:http://zduck.com/2012/powershell-batch-files-exit-code/ – djangofan