简体   繁体   English

PowerShell-通过Invoke-Command发送变量

[英]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.

相关问题 Powershell Invoke-Command的问题 - Issues with Powershell Invoke-Command 在 Powershell 中通过 Invoke-Command 运行脚本时无法识别“Get-IISAppPool” - "Get-IISAppPool" not recognised when script is run via Invoke-Command in Powershell 如何在远程计算机上通过 powershell invoke-command 使用 psexec? (无效句柄) - Howto use psexec via powershell invoke-command on a remote computer? (invalid handle) Powershell的Invoke-Command不会接受-ComputerName参数的变量 - Powershell's Invoke-Command won't take in a variable for -ComputerName parameter PowerShell:使用Invoke-Command执行命令错误? - PowerShell: error executing command using Invoke-Command? 使用Powershell的Invoke-Command调用带参数的批处理文件 - Using Powershell's Invoke-Command to call a batch file with arguments 从 linux 的 powershell invoke-command 获取退出代码 - Get exitcode from powershell invoke-command from linux 使用调用命令调用.bat文件的Powershell错误处理 - Powershell error handling using invoke-command calling .bat file 使用Powershell使用Invoke-Command远程安装Python失败 - Remote Installation of Python using Powershell fails using Invoke-Command 使用PowerShell Invoke-Command和Process使stdout出现在控制台上 - Getting stdout to appear on console with PowerShell Invoke-Command and Process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM