【问题标题】:Powershell remoting - creating objects remotelyPowershell 远程处理 - 远程创建对象
【发布时间】:2023-03-19 08:57:01
【问题描述】:

我正在尝试创建对象并使用远程处理对其进行操作,如下所示:

$foldername = "C:\test"
$computername ="remotecomputer"

Invoke-Command -computername $computername -Scriptblock {$newquotaobj = New-Object -ComObject Fsrm.FsrmQuotaManager}
Invoke-Command -computername $computername -Scriptblock {$newquotasrc = $newquotaobj).GetQuota($Using:foldername)}

我的理解是 $newquotaobj 将被反序列化并发回 - 但它似乎没有发生。是否有可能在这里实现我的目标 - 即远程创建 com 对象并对其进行操作?

【问题讨论】:

    标签: powershell remoting


    【解决方案1】:

    Invoke-Command 返回输出,而不是创建的对象。如果你想通过Invoke-Command远程操作COM对象,你必须在脚本块中包含代码:

    $foldername = "C:\test"
    $computername ="remotecomputer"
    
    Invoke-Command -ComputerName $computername -ScriptBlock {
      $newquotaobj = New-Object -ComObject Fsrm.FsrmQuotaManager
      $newquotasrc = $newquotaobj.GetQuota($args[0])
      $newquotasrc   # <-- this will be returned to the local host
    } -ArgumentList $foldername
    

    【讨论】:

    • 非常感谢!现在一切正常,我可以远程创建配额:) Invoke-Command -session $s -Scriptblock { $fsrmremote = New-Object -ComObject Fsrm.FsrmQuotaManager $qcreate = $fsrmremote.CreateQuota($Using:foldername) $qcreate.Description = $Using:qdescription $qcreate.QuotaLimit = $Using:qlimitBytes $qcreate.Commit() }
    猜你喜欢
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    相关资源
    最近更新 更多