标签: powershell

尝试Catch在Powershell脚本中不起作用

我似乎无法使这个try-catch起作用。我敢肯定这很简单,但此刻我的大脑太炸了。请帮忙!

param(
[String[]]$RemoteServicesVMs = ('VmThatThrowsError')
)

function getWinServiceStatus
{

#Get-WmiObject "win32_service" 
    try{

        Get-WmiObject "win32_service" | Where-Object {$_.startname -notlike "NT*" -and $_.startname -notlike "local*" } | Format-Table -property PSComputerName, name, state, status, startname

    }  
    catch{

        wite-host "Failed"

    }

}

$PassWordEnc = convertto-securestring $RemotePassWord -asplaintext -force
$MyCred = New-Object -TypeName System.Management.Automation.PSCredential ArgumentList $RemoteUserName,$PassWordEnc

foreach($RemoteServicesVM in $RemoteServicesVMs){

    Invoke-Command -ComputerName $RemoteServicesVM -Port 5985 -Authentication Negotiate -ScriptBlock ${function:getWinServiceStatus} -Credential $MyCred

}
Run Code Online (Sandbox Code Playgroud)

powershell try-catch

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

在PowerShell中协助使用可选的Regex

我试图解析软件,版本号,并在日期安装从这样的数据:

AXIS Media Control Embedded
AXIS Media Control 5.60 Redist  [installed on 2014/05/28]
Cisco WebEx Meetings
Adobe Flash Player 13 ActiveX  [version 13.0.0.214]
Adobe Flash Player 13 Plugin  [version 13.0.0.214]
Bullzip PDF Printer 9.3.0.1516  [version 9.3.0.1516]  [installed on 2014/05/12]
Security Management System (Client)  [version 7.0.1.0]  [installed on 2014/05/28]
Symmetry7500 Plugin  [version 1.3]  [installed on 2014/05/28]
Cross Match Transmission Manager  [version 3.8.9.0012]  [installed on 2014/05/08]
Cross Match Live Scan Management System  [version 8.4.5.0031]  [installed on 2014/05/08]
System Center Endpoint Protection  [version …

regex powershell

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

New-Object:找不到"PSCredential"的重载和参数计数:"2"

我希望将电子邮件发送给多个收件人,同时我也不想提示输入用户名和密码.所以我使用了下面的字符串转换,但后来我面临以下错误消息.

你能否建议你的答案来纠正这个问题?

[string] [ValidateNotNullOrEmpty()] $secpasswd = "Q$$777LV"
$secpasswd = ConvertTo-SecureString -String "Q$$777LV" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (“test”, $secpasswd)
Run Code Online (Sandbox Code Playgroud)

错误信息:

New-Object:找不到"PSCredential"的重载和参数计数:"2"

powershell credentials

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

New-AzureRmResourceGroup:'this.Client.SubscriptionId'不能为null

我正在尝试从powershell在Azure中创建一个新的Web应用程序服务,但遇到以下错误:

New-AzureRmResourceGroup : 'this.Client.SubscriptionId' cannot be null.

$webAppName = "powershelldemowebapp"
$ResourceGroupName = "PowerShellResourceGroup"          
$Location = "East Asia"

Login-AzureRmAccount -ServicePrincipal -Tenant 000000-0000-0000-0000-00000 -Credential $psCred
Get-AzureSubscription
Select-AzureSubscription  -SubscriptionId 00000-0000-0000-000-0000
New-AzureRmWebApp -Name $webAppName  -ResourceGroupName $ResourceGroupName  -Location $Location 
Run Code Online (Sandbox Code Playgroud)

powershell azure alm azure-web-sites azure-powershell

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

如何从调用Invoke-WebRequest访问JSON?

我需要调用API(从我的DNS提供程序获取记录列表),这可以使用curl完成.这将返回带有我所有记录的格式化的json

curl -H 'Authorization: Bearer mytoken' -H 'Accept: application/json' https://api.dnsimple.com/v2/12345/zones/example.com/records
Run Code Online (Sandbox Code Playgroud)

但是,我需要能够从PowerShell执行此操作

$uri = "https://api.dnsimple.com/v2/12345/zones/example.com/records"
$headers = @{}
$headers["Authorization"] = "Bearer mytoken"
$headers["Accept"] = "application/json"
$foo = Invoke-WebRequest $uri -Headers $headers 
Run Code Online (Sandbox Code Playgroud)

这个命令运行,但在$ foo中我可以访问返回的JSON吗?

powershell json curl

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

Powershell Get-Childitem将子目录排除在目录中

大家好我的文件夹结构如下

D:\Exclude
     Include
     Include1
         Exclude
         Include 
Run Code Online (Sandbox Code Playgroud)

我需要的是我想过滤目录Include1\Exclude这是我尝试过的无法正常工作的目录

$Path ="D:\Exclude"
$DirList = Get-ChildItem -Path "$Path" -Recurse | where {$_.DirectoryName -ne "D:\Exclude\Include1\Exclude"}
$DirList
Run Code Online (Sandbox Code Playgroud)

powershell

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

在PowerShell中将十六进制转换为ASCII

我有一系列十六进制值,如下所示:

68 65 6c 6c 6f 57 6f 72 6c 64 7c 31 2f 30 38 31 35 7c 41 42 43 2d 31 35 02 08
Run Code Online (Sandbox Code Playgroud)

我现在需要将此十六进制值转换为ASCII,以便结果如下所示:

helloWorld|1/0815|ABC-15
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西,但我从未进入最终的代码.我尝试以任何可以想象的方式使用convert-function而没有任何成功.

目前我使用此网站 进行转换,但我需要在PowerShell脚本中执行此操作.

powershell hex ascii string-formatting

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

Powershell posh-ssh:如何存储Invoke-SSHCommand的结果?

Invoke-SSHCommand关闭连接后,如何存储使用结果?

我尝试了两种不同的方式:

#method 1
$result = Invoke-SSHCommand -Index 0 -Command "ls /somepath"

#method 2
Invoke-SSHCommand -Index 0 -Command "ls /somepath" -OutVariable $result
Run Code Online (Sandbox Code Playgroud)

$result两种方法后变量都是空的

ssh powershell

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

更改CSV文件列中的值

我正在尝试使用PowerShell找到一种方法来更改csv文件中"可用人员"列中所有条目的值.

如果值为'Y',则应将其更改为'1',如果值为'N',则应将其更改为')':

Branch Number,      CoreID,     Available Person,      Workstation
8002,           FMD354800200,   Y,
8002,           FMD354800201,   Y,
8002,           FMD354800202,   N,
8002,           FMD354800203,   N,
8002,           FMD354800204,   Y,

这是我尝试过的:

$csv=Import-Csv user.csv' | $csv | %{ if($_.'Available Person' -eq "Y") {$_.'Available Person'="1"} } $csv|export-csv user1.csv' -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

csv powershell

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

Where-object传递变量不适用于块脚本

我的$var变量不适用于远程计算机.我在这个链接中使用过滤器在powershell中将变量传递给where-object不起作用.但我的脚本仍然无法从我的输入中找到应用程序.

$var = "application" 

Invoke-command -ComputerName $cpu {
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* ,
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object {$_.DisplayName -like "*$var*"}
Run Code Online (Sandbox Code Playgroud)

powershell

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