我正在尝试Azure Table Storage API使用 Powershell执行,Invoke-RestMethod但它返回415 error。
这是Powershell代码:
$accessKey = "accesskey"
$account_name = "storage account"
$table_name = "table storage"
$date = "Fri, 10 Nov 2017 00:47:55 GMT"
$api_version = "2015-12-11"
$table_url = "https://$account_name.table.core.windows.net/$table_name"
$data_type = "application/json"
$signature = "generated signature"
$body = @{
Address="Mountain View";
Age="23"
}
$json = $body | Convertto-JSON
$table_headers = @{
"x-ms-date" = $date
"x-ms-version" = $api_version
"Authorization" = "SharedKey $account_name`:$signature"
"Content-Type" = $data_type
}
Invoke-RestMethod -Method POST -Uri $table_url -Headers …Run Code Online (Sandbox Code Playgroud) 我正在尝试在PowerShell 2.0中执行基本的后台作业,我看到了start-job和invoke-command -asjob的不同内容.
如果我这样做:
start-job -scriptblock {get-process}
我得到一个job对象,但是子作业(由start-job自动创建)总是有一个JobStateInfo为"NotStarted".
但是,这可以按预期工作:
invoke-command -scriptblock {get-process} -computer localhost -asjob
我已经运行了enable-psremoting ....我还需要做些什么才能让后台工作正常工作?
我有一组PowerShell脚本,其中包含位于同一文件夹中的"常用"脚本,如下所示:
# some-script.ps1
$scriptDir = Split-Path -Parent $myinvocation.mycommand.path
. "$scriptDir\script-utils.ps1"
Run Code Online (Sandbox Code Playgroud)
如果直接调用脚本,这很好,例如
.\some-script.ps1
Run Code Online (Sandbox Code Playgroud)
但是,如果使用Invoke-Command调用脚本,则不起作用:
Invoke-Command -ComputerName server01 -FilePath "X:\some-script.ps1"
Run Code Online (Sandbox Code Playgroud)
在这种情况下,实际上,$myinvocation.mycommand包含脚本的内容,并且$myinvocation.mycommand.path为null.
如何使用Invoke-Command调用脚本时,如何确定脚本的目录?
注意
最后,这是我实际使用的解决方案:
Invoke-Command -ComputerName server01 `
{param($scriptArg); & X:\some-script.ps1 $scriptArg } `
-ArgumentList $something
Run Code Online (Sandbox Code Playgroud)
这也允许将参数传递给脚本.
我希望能够远程进入系统并在那里压缩或解压缩文件,并在完成时获得过程信号.启动过程使用-wait参数从PowerShell同步运行7z.exe.当我尝试将它与invoke-command结合使用以远程运行相同的命令时,它不支持wait参数,我相信它会杀死进程,因为它快速返回并且从不生成zip文件.
[string]$sevenZip = "C:\Program Files\7-zip\7z.exe"
[Array]$arguments = "a", $zipFilename, $dirToZip;
"Starting $sevenZip with $arguments"
Start-Process $sevenZip "$arguments" -Wait
#blocks and waits for zip file to complete
Run Code Online (Sandbox Code Playgroud)
我最初尝试过PSCX write-zip和expand-archive,但后者与64位.NET 4.0配置不兼容.所以现在我试图通过命令行调用64位7z.exe.我没有收到任何错误.PowerShell将作业报告为运行状态,然后完成,并且不会生成任何zip文件.
Invoke-Command -ComputerName localhost -FilePath 'C:\Scripts\ZipIt.ps1' -ArgumentList 'd:\TestFolder','d:\promote\TestFile.7z' -AsJob
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助或指针.
谢谢,格雷戈里
使用 PowerShell 3.0,我使用 Invoke-Command cmdlet 将 xmlElement 传递给脚本块。问题是我认为 scriptBlock 将参数作为 arrayList 而不是作为 xmlElement 接收(实际上,我知道收到的参数是ArrayList我注销时的$service.GetType())。
这是我的 XMLElement:
<Service>
<ServiceName>Spooler</ServiceName>
<Startup>Disabled</Startup>
</Service>
Run Code Online (Sandbox Code Playgroud)
这是我的脚本块:
$scptModifyService = {
param (
$service
);
Set-Service -Name $service.ServiceName -StartupType $service.Startup;
}
Run Code Online (Sandbox Code Playgroud)
这是我调用 scriptBlock 的代码:
## Modify remote Services:
foreach($service in $XML.QCSettings.Services.Service) {
Invoke-Command -ScriptBlock $scptModifyService -ComputerName $ComputerName -ArgumentList $service;
}
Run Code Online (Sandbox Code Playgroud)
根据我对 Invoke-Command cmdlet (get-help invoke-command) 的理解,ArgumentList 可以将一个对象作为输入,所以我认为这不是我如何在“foreach”中调用脚本块的问题'循环上面。希望这是有道理的。
有没有人知道如何将我的脚本块中的参数作为 xmlElement 而不是作为ArrayList.
我已经尝试了几件事(在我的脚本块中):
param( $service = [xml]Get-Content args[0] );
Run Code Online (Sandbox Code Playgroud)
以及该代码的其他变体。
出于某种原因,在远程计算机上通过invoke命令运行时,Start-Process msiexec将无法工作.我查了一下,虽然有些人建议使用psiexec我看到很多人使用普通的旧invoke-command在远程机器上启动msi安装程序.
这是我目前使用的代码:
$session = New-PSSession -computername $computerName -ea stop
$command = {
Param(
[Parameter()]
[string]$computerName,
[Parameter()]
[string]$domain,
[Parameter()]
[string]$user,
[Parameter()]
[string]$password,
[Parameter()]
[string]$installDir
)
$msiArgumentList = "/i C:\Installer.msi /l c:\log.txt /quiet /qr /norestart IAGREE=Yes DOMAIN=$domain ACCOUNT=$user PASSWORD=$password PASSWORDCONFIRM=$password INSTALLDIR=$installDir"
Start-Process msiexec -ArgumentList $msiArgumentList -Wait
}
Invoke-Command -session $session -ScriptBlock $command -ArgumentList $computerName, $domain, $user, $password, $installDir
Remove-PSsession -session $session
Run Code Online (Sandbox Code Playgroud)
我使用相同的方法使用intallutil远程安装服务,它工作正常.在目标计算机上启用脚本以及远程处理,以便所有帐户都能正常工作.两台计算机都具有相同的凭据,但我仍尝试向invoke-command和pssession添加凭据.我在本地测试了代码,安装工作正常.远程它没有,也没有错误.我可以在taskmanager的目标机器上看到msiexec已启动但没有任何反应.我甚至试过禁用防火墙,但仍然没有.我试过&运算符启动msiexec但仍然没有.
不确定我还能尝试什么.
如何使用哈希表中收集的参数与ArgumentListon 一起使用Invoke-Command?
$CopyParams = @{
Source = 'E:\DEPARTMENTS\CBR\SHARE\Target'
Destination = 'E:\DEPARTMENTS\CBR\SHARE\Target 2'
Structure = 'yyyy-MM-dd'
}
Invoke-Command -Credential $Cred -ComputerName 'SERVER' -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList @CopyParams
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,它总是抱怨'来源':
Cannot validate argument on parameter 'Source'. The "Test-Path $_" validation script for the argument with
value "System.Collections.Hashtable" did not return true. Determine why the validation script failed
Run Code Online (Sandbox Code Playgroud)
这篇博客谈到了类似的问题,但我无法让它发挥作用.
对于一个简单的Copy-Item内部Invoke-Command例子来说也是如此:
Invoke-Command -Credential $Cred -ComputerName 'SERVER' -ScriptBlock {Copy-Item} -ArgumentList @CopyParams
Invoke-Command : Missing an argument for …Run Code Online (Sandbox Code Playgroud) 我正在编写一个脚本来停止和启动两个远程服务器中的服务.这是我的问题,
在我的脚本中,我做了new-pssession并使用invoke-command来停止和启动服务.
我需要使用enter-pssession吗?
更新: 这是我的脚本需要做的事情.
在server1上,我需要停止并启动两个服务.在server2上,我需要停止并启动一项服务.
# foreach for server 1 since I need to stop and start two services. created a session for server 1
foreach($service in $services){
$session = New-PSSession -ComputerName $serverName -Credential $cred
Invoke-Command -Session $session -ScriptBlock {param($service) Stop-Service -Name $service} -ArgumentList $service
remove-pssession -session $session
}
# created a session for server 2. I need to stop and start just one service in server 2
$session = New-PSSession -ComputerName $serverName -Credential $cred
Invoke-Command -Session $session -ScriptBlock …Run Code Online (Sandbox Code Playgroud) 我想传递给脚本的所有参数并执行。
例如,给定execute.ps1脚本:
Invoke-Expression $($args[0])
Run Code Online (Sandbox Code Playgroud)
我可以跑:
.\execute.ps1 hostname
myhostname
Run Code Online (Sandbox Code Playgroud)
与此脚本的两个参数相同:
Invoke-Expression "$($args[0]) $($args[1])"
Run Code Online (Sandbox Code Playgroud)
并通过以下方式执行它:
.\execute.ps1 echo foo
foo
Run Code Online (Sandbox Code Playgroud)
如何使脚本通用以支持未知数量的参数?
例如:
.\execute.ps1 echo foo bar buzz ...
Run Code Online (Sandbox Code Playgroud)
我尝试了以下失败的组合:
Invoke-Expression $args
Run Code Online (Sandbox Code Playgroud)
调用表达式:无法将“System.Object[]”转换为参数“Command”所需的类型“System.String”。不支持指定的方法。
Invoke-Expression [system.String]::Join(" ", $args)
Run Code Online (Sandbox Code Playgroud)
调用表达式:找不到接受参数“System.Object[]”的位置参数。
Invoke-Expression $args.split(" ")
Run Code Online (Sandbox Code Playgroud)
调用表达式:无法将“System.Object[]”转换为参数“Command”所需的类型“System.String”。不支持指定的方法。
Invoke-Expression [String] $args
Run Code Online (Sandbox Code Playgroud)
调用表达式:找不到接受参数“System.Object[]”的位置参数。
我正在尝试运行 invoke-commandpowershell script in a powershell file在remote computer. 我正在使用credentials for a user with Administrator privilege. 该命令需要由 执行running powershell as an administrator。我尝试使用 powershell 脚本调用的应用程序存在许可问题,因此我无法更改管理员凭据,但需要使用该特定用户本身运行。我尝试-RunAsAdministrator在 Invoke-Command 末尾使用,但出现错误:
Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
$command = {
cd Folder
C:\Folder\build.ps1
}
Invoke-Command -ComputerName $RemoteSystemIP -ScriptBlock $command -credential $Credentials1 -ErrorAction Stop -AsJob
Run Code Online (Sandbox Code Playgroud)
我试图将其作为后台作业执行,这就是我添加参数的原因-AsJob。
已经好几天了,我还没有找到解决方案。