【发布时间】:2016-07-26 23:45:07
【问题描述】:
我想知道是否可以通过 PowerShell 远程删除队列?我有以下脚本:
cls
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$computers = @("comp1","comp2","comp3");
foreach($computer in $computers) {
$messageQueues = [System.Messaging.MessageQueue]::GetPrivateQueuesByMachine($computer);
foreach ($queue in $messageQueues) {
$endpoint = [string]::Format("FormatName:DIRECT=OS:{0}\{1}", $computer, $queue.QueueName);
Write-Host $endpoint
[System.Messaging.MessageQueue]::Delete($endpoint);
}
}
如果我在要删除其队列的机器上运行它,这很好用,但是当我远程运行它时,我得到了错误:
The specified format name does not support the requested operation. For example, a direct queue format name cannot be deleted.
如果可以做到这一点,有什么想法吗?
编辑
奇怪的是,我发现我可以通过 PowerShell 远程访问机器并执行脚本块。但是,我不明白这样做之间的区别:
这个:
$endpoint = [string]::Format("FormatName:DIRECT=OS:{0}\{1}", $computer, $queue.QueueName);
Invoke-Command -ComputerName $computer -ScriptBlock { [Reflection.Assembly]::LoadWithPartialName("System.Messaging"); [System.Messaging.MessageQueue]::Delete($endpoint) };
还有这个:
Invoke-Command -ComputerName $computer -ScriptBlock { [Reflection.Assembly]::LoadWithPartialName("System.Messaging"); [System.Messaging.MessageQueue]::Delete("FormatName:DIRECT=OS:MY_SERVER\some.endpoint") };
$endpoint 的值是相同的,但是由于某些奇怪的原因,它不喜欢变量方法,尽管两个值是相同的。我通过设置$endpoint 然后调用delete 对此进行了测试。我得到错误:
Exception calling "Delete" with "1" argument(s): "Invalid value for parameter path."
我想说的是,如果我将值硬编码为它工作的参数的一部分,但将它分配给一个变量然后调用我得到一个错误的方法
【问题讨论】:
-
可能想签出this。错误消息可能不是说需要不同的名称格式。
标签: powershell msmq