[英]PowerShell - send variable via Invoke-Command
I have 3 Windows 2016 servers and some routine task I want to automate. 我有3台Windows 2016服务器和一些我想自动化的例行任务。 I'm newbie in PowerShell, so I spent a lot of time looking for answer in StackOverflow and "googling".
我是PowerShell的新手,所以我花了很多时间在StackOverflow和“ googling”中寻找答案。
My task is to backup some files via 7zip on remote server. 我的任务是通过远程服务器上的7zip备份一些文件。
There are is a command which works great: 有一个很棒的命令:
Invoke-Command -ComputerName 10.10.0.20 -ScriptBlock {start-Process -wait-FilePath 'C:\Program Files\7-Zip\7z.exe' -ArgumentList 'a','-t7z','C:\BlueCollar_backup\bluecollar_121.zip','C:\Services\BlueCollar' -wait}
But I need to add date to bluecollar_121.zip archive name, it should look like bluecollar_13.08.2018.zip 但是我需要在bluecollar_121.zip档案名称中添加日期,它看起来应该像bluecollar_13.08.2018.zip
I've tried a huge amount of variants but with no luck. 我尝试了很多变体,但是没有运气。
[string]$stime = get-date -f 'dd.MM.yyyy
Invoke-Command -ComputerName 10.10.0.20 -ScriptBlock {start-Process -FilePath 'C:\Program Files\7-Zip\7z.exe' -ArgumentsList 'a -t7z C:\BlueCollar_backup\bluecollar_'$stime'.zip C:\Services\BlueCollar' -wait}
Second try was: 第二次尝试是:
[string]$stime = get-date -f 'dd.MM.yyyy
[string]$tmp1="C:\BlueCollar_backup\bluecollar_$stime.zip"
$command = {start-Process -FilePath 'C:\Program Files\7-Zip\7z.exe' -ArgumentsList 'a -t7z $stime C:\Services\BlueCollar' -wait}
And the last try: 最后一次尝试:
[string]$stime = get-date -f 'dd.MM.yyyy
$arguments = @()
$arguments += "a"
$arguments += "-t7z"
$arguments += "$tmp1"
$arguments += "C:\Services\BlueCollar"
Invoke-Command -ComputerName 10.10.0.20 -ScriptBlock {start-Process -FilePath 'C:\Program Files\7-Zip\7z.exe' -ArgumentList($arguments) -wait}
They all aren't working. 他们都没有工作。 The problem is $stime variable.
问题是$ stime变量。 How can I put variable $stime into Invoke-Command ?
如何将变量$ stime放入Invoke-Command中?
UPD1: I solved that problem! UPD1:我解决了这个问题!
Invoke-Command -ComputerName '10.10.0.20' -ArgumentList $stime -ScriptBlock{
$stime = $args[0]
start-Process -FilePath 'C:\Program Files\7-Zip\7z.exe' "a -t7z C:\BlueCollar_backup\bluecollar_$stime.zip C:\Services\BlueCollar" -wait
}
The problem is that the computer you are executing the command from knows about the $stime variable, but the remote computer doesn't know it. 问题是您从中执行命令的计算机知道$ stime变量,但是远程计算机却不知道。 So you have to pass it in.
因此,您必须将其传递。
There are a couple options, one is to use the $Using variable 有两种选择,一种是使用$ Using变量
[string]$stime = get-date -f 'dd.MM.yyyy
Invoke-Command -ComputerName 10.10.0.20 -ScriptBlock {
start-Process -FilePath 'C:\Program Files\7-Zip\7z.exe' -ArgumentsList "a -t7z C:\BlueCollar_backup\bluecollar_$($Using:stime).zip C:\Services\BlueCollar" -wait
}
Another option that is perhaps a little cleaner looking is to make your ScriptBlock a string of text, and then pass it to the remote computer: 也许更简洁一点的另一个选择是使ScriptBlock成为文本字符串,然后将其传递给远程计算机:
$ScriptBlock=[ScriptBlock]::Create("start-Process -FilePath 'C:\Program Files\7-Zip\7z.exe' -ArgumentsList 'a -t7z C:\BlueCollar_backup\bluecollar_$stime.zip C:\Services\BlueCollar'")
Invoke-Command -ComputerName 10.10.0.20 -ScriptBlock $ScriptBlock
An important distinction is between single quotes and double quotes. 一个重要的区别是单引号和双引号之间。 When you use single quotes, it resolves text exactly as is.
使用单引号时,它完全按原样解析文本。 When you use double quotes, it resolves variables.
使用双引号时,它将解析变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.