I have 3 Windows 2016 servers and some routine task I want to automate. I'm newbie in PowerShell, so I spent a lot of time looking for answer in StackOverflow and "googling".
My task is to backup some files via 7zip on remote server.
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
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. How can I put variable $stime into Invoke-Command ?
UPD1: I solved that problem!
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
}