使用powershell从命令行运行带有参数和开关的字符串

时间:2017-06-19 14:32:30

标签: powershell command-line

我有一个powershell脚本可以将文件复制到远程计算机,我需要从命令行和参数/参数执行它。

这就是我所拥有的......我无法让它发挥作用:

Get-Content C:/temp/list.txt |
ForEach-Object {
    Try {
        $source = "\\server\folder\my application 1.2.1.EXE"
        $destination = "\\" + $_ + "\C$\temp\"
        New-Item -Path  $destination -ItemType Directory -Force
        Copy-Item -path $source -Destination $destination  -force | Out-Null
        & "$_ +"\C:\temp\my application 1.2.1.EXE" Location=07 /s" ##| Out-Null

        $status = "Success"
        }
    Catch {
        $status = "Failed"
        }
    "$_, $status" | out-file -filepath c:\temp\restult1.csv -Append -Encoding ascii
}

1 个答案:

答案 0 :(得分:0)

我解决了。

Get-Content C:/temp/list.txt |
ForEach-Object {
    Try {
        $source = "\\server\folder\my application 1.2.1.EXE"
        $destination = "\\" + $_ + "\C$\temp\"
        New-Item -Path  $destination -ItemType Directory -Force
        Copy-Item -path $source -Destination $destination  -force | Out-Null
        Invoke-Command -ComputerName $_ -ScriptBlock {
        & cmd.exe /c "C:\temp\my application 1.2.1.EXE" Location=07 /s ##| Out-Null
        }
        $file = $destination + "my application 1.2.1.EXE"
        Remove-Item $file -Force
        $status = "Success"
        }
    Catch {
        $status = "Failed"
        }
    "$_, $status" | out-file -filepath c:\temp\restult1.csv -Append -Encoding ascii
}
相关问题