使用 Invoke-Command Powershell 命令的 -ScriptBlock 中的参数调用函数
我有两台服务器,其中一台是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
I have two server one of them is Active directory and other is Windows10.
I want to write a Powershell script that delete and make a directory.(for easy situation I choose delete and make directory but in real, The script used for getting group and organization unit of active directory.)
I write three function and below Powershell script. the first one is for run command on remote server and two of them for create and delete a directory. The 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
When I run this script as below I got errors:
my_powershell_script.ps1 -choosen_function make_directory -username admin -password admin -remote_address 192.168.2.22
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使该脚本正常工作,需要更改一些内容。
run_commend_on_remote_server
函数中,您使用的是用于交互式会话的Enter-PSSession
,然后您使用的是Invoke-Command
,在这种情况下,如果我正确理解您的意图,Enter-PSSession
必须删除。Invoke-Command
脚本块内,正在使用$choosen_function
,但是尚未在该范围内定义 2 个辅助函数 (远程范围)。如果您想远程使用函数,则需要传递函数的定义。同样的情况也适用于$absolute_path
。为了将本地定义的变量传递到远程作用域,您可以使用-ArgumentList
或$using:
,请参阅 示例 9 来自Invoke-Command
文档。&
或 点采购运算符.
来调用它,即:& $choosen_function
。UserName
和Password
作为脚本的参数传递,而是调用Get-Credential
在脚本内。-absolute_path
被用作run_commend_on_remote_server
的参数,但该函数没有这样的参数。了解了这些要点后,您可以按照以下方式处理脚本:
There are a few things which need to be changed in order for this script to work.
run_commend_on_remote_server
function, you're usingEnter-PSSession
which is meant for interactive sessions, and then you're usingInvoke-Command
, in this case if I understand your intent correctlyEnter-PSSession
has to be removed.Invoke-Command
script blocks's,$choosen_function
is being used however the 2 helper functions have not been defined on that scope (remote scope). You need to pass the definition of your functions if you want to use them remotely. Same thing applies for$absolute_path
. In order to pass locally defined variables into the remote scope, you can use either-ArgumentList
or$using:
, see Example 9 from theInvoke-Command
Doc.&
or the dot sourcing operator.
to invoke it, i.e.:& $choosen_function
.UserName
andPassword
as argument of your script, I would personally recommend you to callGet-Credential
inside your script.-absolute_path
is being used as parameter forrun_commend_on_remote_server
but the function does not have such parameter.With these points being understood, here is how you could approach your script: