我们在这里有一些家庭酿造的窗户服务.其中一个是有问题的,因为当被问到时它不会总是停止.它有时会陷入"停止"状态.
使用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 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
但错误一直存在.任何人都可以帮忙,因为我已经用尽所有谷歌选项.谢谢.
我在.net中实现了客户cmdlet.我想知道用户传递给它的所有参数.
My-Cmdlet -foo -bar -foobar
Run Code Online (Sandbox Code Playgroud)
基本上我想知道用户以编程方式使用参数foo,bar,foobar执行此cmdlet.
在脚本中看起来我们可以使用:$ PSBoundParameters.ContainsKey('WhatIf')
我需要.net(c#)中的那个.
有没有办法让我可以将一个类扩展cmdlet写入powershell控制台而不将其写入管道?
我正在编写一个cmdlet,它会将大量对象写入管道(然后人们将管道输入另一个cmdlet,如Export-CSV).问题是,如果我开始将信息写入控制台,也会被提起.
例如,对象1对象2"我是通过this.WriteObjct对象3记录到powershell的信息消息
如果我尝试将cmdlet管道传输到另一个cmdlet中,我将获得对象1,2,信息消息和3.我需要它,因此它只管道对象1,2和3.
谁能指出我正确的方向?
执行以下脚本(这是实际脚本的一部分)时,我在 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) 我成功地向我的 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和 …
我读到了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) 我试图使用powershell脚本过滤服务器中运行的服务.但语法似乎不正确
脚本
Get-Service -ComputerName $ ServerName | Where-Object {$ _.Name -like"DEX*" - 或$ _.Name -like"WORLD*" - 或$ _.Name -like"Entr*"}
突出显示的部分有问题.任何帮助都很赞赏..
我创建了一个非常简单的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)
我的脚本有什么问题?