标签: cmdlet

Windows服务和Stop-Process cmdlet的有趣问题

我们在这里有一些家庭酿造的窗户服务.其中一个是有问题的,因为当被问到时它不会总是停止.它有时会陷入"停止"状态.

使用powershell,我们正在检索其PID并使用Stop-Process cmdlet来终止相关进程,但这也无法正常工作.

相反,我们得到一条关于名为服务的消息,该服务System.ServiceProcess.ServiceController.Name显然不是我们的服务,但它引用的是PID.

以下是我们为停止服务而采取的措施.首先,我们使用Get-Service cmdlet:

$ServiceNamePID = Get-Service -ComputerName $Computer | where { ($_.Status -eq 'StopPending' -or $_.Status -eq 'Stopping') -and $_.Name -eq $ServiceName}
Run Code Online (Sandbox Code Playgroud)

然后,使用该ServiceNamePID,我们获取PID并在Stop-Process cmdlet中使用它

$ServicePID = (get-wmiobject win32_Service -ComputerName $Computer | Where { $_.Name -eq $ServiceNamePID.Name }).ProcessID
Stop-Process $ServicePID -force
Run Code Online (Sandbox Code Playgroud)

这就是Stop-Process cmdlet 根据任务管理器发出的Cannot find a process with the process identifier XYZ,当实际上PID XYZ 是服务的正确进程ID时.以前有人见过这样的问题吗?

powershell windows-services process cmdlet

2
推荐指数
1
解决办法
7020
查看次数

Appfabric Powershell错误"术语'Get-CacheStatistics'未被识别为cmdlet的名称.."

我正在调用简单的Powershell AppFabric命令:

powershell -noexit c:\scripts\ApplyClusterConfig.ps1
Run Code Online (Sandbox Code Playgroud)

并且脚本只包含:

Get-CacheStatistics
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

The term 'Get-CacheStatistics' is not recognized as the name of a cmdlet, funct
ion, script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
At C:\scripts\ApplyClusterConfig.ps1:1 char:20
+ Get-CacheStatistics <<<<
    + CategoryInfo          : ObjectNotFound: (Get-CacheStatistics:String) [],
    CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

我已经完成了我可以在网上找到的所有内容,包括

Set-ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)

并使用.\来引用文件:

powershell -noexit .\ApplyClusterConfig.ps1
Run Code Online (Sandbox Code Playgroud)

并设置环境路径以包含c:\ Scripts

但错误一直存在.任何人都可以帮忙,因为我已经用尽所有谷歌选项.谢谢.

powershell cmdlet appfabric

1
推荐指数
1
解决办法
4537
查看次数

如何以编程方式知道传递给cmdlet的所有参数?

我在.net中实现了客户cmdlet.我想知道用户传递给它的所有参数.

My-Cmdlet -foo -bar -foobar
Run Code Online (Sandbox Code Playgroud)

基本上我想知道用户以编程方式使用参数foo,bar,foobar执行此cmdlet.

在脚本中看起来我们可以使用:$ PSBoundParameters.ContainsKey('WhatIf')

我需要.net(c#)中的那个.

c# powershell cmdlets cmdlet powershell-2.0

1
推荐指数
1
解决办法
1199
查看次数

cmdlet写入控制台而不写入管道

有没有办法让我可以将一个类扩展cmdlet写入powershell控制台而不将其写入管道?

我正在编写一个cmdlet,它会将大量对象写入管道(然后人们将管道输入另一个cmdlet,如Export-CSV).问题是,如果我开始将信息写入控制台,也会被提起.

例如,对象1对象2"我是通过this.WriteObjct对象3记录到powershell的信息消息

如果我尝试将cmdlet管道传输到另一个cmdlet中,我将获得对象1,2,信息消息和3.我需要它,因此它只管道对象1,2和3.

谁能指出我正确的方向?

c# powershell cmdlet

1
推荐指数
1
解决办法
2669
查看次数

术语“xsd”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称

执行以下脚本(这是实际脚本的一部分)时,我在 powershell 中收到以下错误消息:

The term 'xsd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:10 char:3

$xsds = ls *.xsd | %{ $_.Name }

if ($xsds.Count -eq 0) { exit }

# Add '.\' to last file name, see http://stackoverflow.com/questions/906093/xsd-exe-output-filename
$last = $xsds | select -last 1
$last = '.\' + …
Run Code Online (Sandbox Code Playgroud)

powershell xsd cmdlet

1
推荐指数
1
解决办法
2244
查看次数

Powershell Add-Member - 但在 JSON 中没有“Value”和“Count”元素

我成功地向我的 JSON 添加了一个成员,但我最终得到了不需要的元素。我要添加的是出现在结果 JSON 中的“Value”中的元素。

{
"Block1": value1,
"Block2": value2,
"Block3": []
}
Run Code Online (Sandbox Code Playgroud)

然后执行 Add-Member cmdlet。

$objectFromJson |
  Add-Member -NotePropertyName "Block3" -NotePropertyValue $newblock -Force
Run Code Online (Sandbox Code Playgroud)

我意识到我不必执行-Force部分,但在我的工作代码中,我的 JSON 字符串使用ConvertFrom-Json解析为一个对象,并且该部分对我的目的有效。

存储在数组中有 1 到 N 个元素$newblock,要序列化为数组值属性Block3

不幸的是,我最终得到以下结果:

{
"Block1": value1,
"Block2": value2,
"Block3": [ { "value": { <elements of $newblock> }, "Count": <n> } ]
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码片段中,<elements of $newblock>表示$newblock数组元素的 JSON 表示,以及数组中元素<n>的数量。

它是有效的 JSON,但不是我想要的。相反,我希望 的元素是数组的$newblock直接元素Block3,而没有带有value和 …

powershell json cmdlet

1
推荐指数
1
解决办法
1561
查看次数

Backup-WDServer:如何指定加密密码

我读到了Web Deploy Powershell Cmdlet,我想开始使用它们.当然,我开始的第一项任务是备份我的测试网络服务器的当前配置状态 - 这个cmdlet应该这样做:

Backup-WDServer -ConfigOnly
Run Code Online (Sandbox Code Playgroud)

但是,当我运行它时,我得到了这个投诉 - 我不确定如何解决它:

Backup-WDServer : The property 'password' located at '/webServer/appHostConfig[@path='']/location[@path='']/section[@name='system.applicationHost/applicationPools']/applicationPools/applicationPoolDefaults/processModel' is 
marked as secure. You must specify an encryption password to archive this property.
At line:1 char:1
+ Backup-WDServer -ConfigOnly
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Backup-WDServer], DeploymentEncryptionException
    + FullyQualifiedErrorId : Microsoft.Web.Deployment.PowerShell.BackupServer
Run Code Online (Sandbox Code Playgroud)

iis powershell cmdlet msdeploy iis-8

0
推荐指数
1
解决办法
1886
查看次数

Powershell where子句过滤

我试图使用powershell脚本过滤服务器中运行的服务.但语法似乎不正确

脚本

Get-Service -ComputerName $ ServerName | Where-Object {$ _.Name -like"DEX*" - 或$ _.Name -like"WORLD*" - 或$ _.Name -like"Entr*"}

突出显示的部分有问题.任何帮助都很赞赏..

powershell cmdlet

0
推荐指数
1
解决办法
1万
查看次数

Powershell字符串长度验证-该脚本不验证输入,但在每次执行中创建一个文件

我创建了一个非常简单的HelloWorld.ps1Power-shell脚本,该脚本接受一个Name参数,验证其长度,然后打印一个hello消息,例如,如果您传递Johnas Name,则应该打印该消息Hello John!

但是奇怪的行为是我每次执行它时:

  • 它不执行验证,因此它接受Name长度超过10个字符的参数。
  • 每次执行它时,它都会创建并更新一个10没有任何扩展名的文件。

这是Power-shell脚本:

param (
    [parameter(Mandatory=$true)]
    [string]
    $Name
)
# Length Validation
if ($Name.Length > 10) {
    Write-Host "Parameter should have at most 10 characters."
    Break
}
Write-Host "Hello $Name!"
Run Code Online (Sandbox Code Playgroud)

这是执行它的命令:

.\HelloWorld.ps1 -Name "John"
Run Code Online (Sandbox Code Playgroud)

我的脚本有什么问题?

powershell cmdlets cmdlet

0
推荐指数
1
解决办法
3920
查看次数