我有兩臺服務器,其中一臺是 Active Directory,另一臺是 Windows10。我想撰寫一個洗掉和制作目錄的Powershell腳本。(為了方便我選擇洗掉和??制作目錄,但實際上,該腳本用于獲取活動目錄的組和組織單元。)
我寫了三個函式和下面的 Powershell 腳本。第一個用于在遠程服務器上運行命令,其中兩個用于創建和洗掉目錄。my_powershell_script.ps1
Param($choosen_function, $username, $password, $remote_address, $absolute_path)
function run_commend_on_remote_server {
param ($choosen_function, $username, $password, $remote_address)
$secpsw = $password | ConvertTo-SecureString -AsPlainText -Force
$credobject = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $secpsw
$psrs = New-PSSession -Credential $credobject -ComputerName $remote_address
Enter-PSSession $psrs
Invoke-Command -ScriptBlock {$choosen_function -absolute_path $absolute_path} -Session $psrs
Exit-PSSession
Remove-PSSession $psrs
}
function delete_directory{
param($absolute_path)
Remove-Item $absolute_path -Recurse
}
function make_directory{
param($absolute_path)
mkdir $absolute_path
}
run_commend_on_remote_server -choosen_function $choosen_function -username $username -password $password -remote_address $remote_address -absolute_path $absolute_path
當我按以下方式運行此腳本時,出現錯誤:
my_powershell_script.ps1 -choosen_function make_directory -username admin -password admin -remote_address 192.168.2.22
uj5u.com熱心網友回復:
為了使該腳本正常作業,需要更改一些內容。
- 在您的
run_commend_on_remote_server功能上,您使用Enter-PSSession的是用于互動式會話的,然后您使用的是Invoke-Command,在這種情況下,如果我正確理解您的意圖,Enter-PSSession則必須洗掉。 - 在
Invoke-Command腳本塊內部的同一函式上,$choosen_function正在使用兩個輔助函式,但是尚未在該范圍(遠程范圍)上定義 2 個輔助函式。如果要遠程使用函式,則需要傳遞函式的定義。同樣適用于$absolute_path. 為了將本地定義的變數傳遞到遠程范圍,您可以使用-ArgumentList或$using:,請參閱檔案中的示例 9 。Invoke-Command
該命令使用
Using范圍修飾符來標識遠程命令中的區域變數。默認情況下,假定所有變數都在遠程會話中定義。Using范圍修飾符是在 PowerShell 3.0 中引入的。
- 由于您要運行的輔助函式的名稱存盤在一個變數中,為了執行它,您需要使用呼叫運算子
&或點源運算子.來呼叫它,即:& $choosen_function。 - 我個人建議您在腳本中呼叫,而不是傳遞
UserName和作為腳本的引數。PasswordGet-Credential -absolute_path被用作引數,run_commend_on_remote_server但函式沒有這樣的引數。
了解了這些要點后,您可以通過以下方式處理您的腳本:
[cmdletbinding()]
param(
[ValidateSet('delete_directory', 'make_directory')]
$choosen_function,
$remote_address,
$absolute_path
)
function delete_directory{
# code here
}
function make_directory{
# code here
}
# store both function's definition, this will be used
# to pass them to the remote scope
$def = @(
${function:delete_directory}.ToString()
${function:make_directory}.ToString()
)
function run_commend_on_remote_server {
param ($choosen_function, $remote_address, $Credential, $absolute_path)
Invoke-Command -ScriptBlock {
# bring the definition of both functions to this scope
$deldir, $makedir = $using:def
# define both functions
${function:delete_directory} = $deldir
${function:make_directory} = $makedir
# invoke the chosen function
& $using:choosen_function -absolute_path $using:absolute_path
} -Credential $Credential -ComputerName $remote_address
}
# Here we call Get-Credential to get the pop-up for username and secure password
$cred = Get-Credential
$params = @{
choosen_function = $choosen_function
credential = $cred
remote_address = $remote_address
absolute_path = $absolute_path
}
run_commend_on_remote_server @params
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434564.html
標籤:电源外壳 powershell 远程处理
上一篇:Powershell基礎版
下一篇:接受部分自動完成
