$secpasswd = ConvertTo-SecureString "Password" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("administrator", $secpasswd)
$Path = "$env:userprofile\AppData\Local"
cd "$Path\telegraf"
$installtelegraf = .\telegraf.exe --service install --config "$Path\telegraf\telegraf.conf"
$start_telegraf = telegraf.exe --service start
$net_telegraf = net start telegraf
Start-Process Powershell.exe -Credential $mycreds -ArgumentList $installtelegraf $start_telegraf $net_telegraf但我不知道我会犯错。我将使用此脚本进行自动化处理,通过组策略将其安装到我们的客户端。
任何帮助都将不胜感激。谢谢。
发布于 2022-09-01 20:47:06
您的目的是将以后使用Start-Process调用的命令行定义为字符串,为此您必须使用引用;例如:
# Without enclosure in '...' you would *instantly* execute the command.
$start_telegraf = 'telegraf.exe --service start'把它们放在一起:
$installtelegraf = "telegraf.exe --service install --config `"$Path\telegraf\telegraf.conf`""
$start_telegraf = 'telegraf.exe --service start'
$net_telegraf = 'net start telegraf'
Start-Process Powershell.exe -Credential $mycreds -ArgumentList @"
$installtelegraf
$start_telegraf
$net_telegraf
"@注意:
$installtelegraf = ...赋值使用),以确保展开(字符串内插),即扩展$Path (用其值替换);嵌入"字符。因此,必须转义为`"。Start-Process,这样可以轻松地将三个变量作为单独的语句传递;或者,您可以使用带有;的单行字符串作为语句分隔符。telegraf.exe调用中删除了telegraf.exe,因为它没有出现在第二个调用中。- Generally, note that the target user must have permission to access the caller's working directory. If not, a different one must be specified via `-WorkingDirectory`.https://stackoverflow.com/questions/73574690
复制相似问题