在Bash中,我可以轻松地做类似的事情
command1 && command2 || command3
Run Code Online (Sandbox Code Playgroud)
这意味着运行command1,如果command1成功运行command2,并且command1无法运行command3.
什么是PowerShell中的等价物?
我有ps1脚本从vmware集群环境中获取一些信息.
在ps1脚本的某个地方需要按ENTER键.
那么,怎么做?
-谢谢
powershell vmware vmware-server powershell-2.0 powershell-1.0
我正在研究我的第一个PowerShell脚本,无法解决循环问题.
我有以下内容,它将重复$ ActiveCampaigns次数:
Write-Host "Creating $PQCampaign1 Pre-Qualified Report"
Invoke-Item "$PQCampaignPath1\PQ REPORT $PQCampaign1.qvw"
Write-Host "Waiting 1 minute for QlikView to update"
sleep -seconds 60 # Wait 1 minute for QlikView to Reload, create Report and Save.
DO{
Write-Host "Daily Qlikview Reports"
Write-Host "Wating for QlikView to create the $PQCampaign1 PQ Report"
Get-Date
Write-Host "Checking...."
sleep -seconds 1
Write-Host ""
Write-Host "Not Done Yet"
Write-Host "Will try again in 5 seconds."
Write-Host ""
sleep -seconds 5
}
Until (Test-Path "$PQCampaignPath1\$PQCampaign1 …Run Code Online (Sandbox Code Playgroud) 我正在Windows 8.1机器上编写PowerShell脚本.尝试使用New-Object cmdlet创建PSCredential对象时,出现此错误:
New-Object:找不到"PSCredential"的重载和参数count:"2".
在另一台Windows 8.1机器上运行完全相同的脚本对我来说很好.我还验证了两台机器都运行相同版本的PowerShell 4.0
两台计算机都安装了相同的.NET Framework 4.0.
知道为什么会这样,以及如何解决这个问题?
$userPassword = ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force
$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "myUserName", $userPassword
Run Code Online (Sandbox Code Playgroud)
经过一些测试后,我发现了问题所在.对于我的功能,我打算从用户那里获取用户名和密码,但如果用户决定跳过这些输入参数,也会提供默认值.
为此我通过在参数部分添加以下行来实现它
[string][ValidateNotNullOrEmpty()] $userPassword = "myPassword",
Run Code Online (Sandbox Code Playgroud)
似乎问题是我在参数中将它定义为[String]但后来试图成为SecureString,这导致了问题.
删除参数中的[String]属性解决了问题.
PS H:\> Invoke-Command -computername SERVERNAME { Get-ChildItem -path E:\dfsroots\datastore2\public} | Where-Object {{ $_.e
xtension-match "xls" -or $_.extension-match "xlk" } -and { $_.creationtime -ge "06/01/2014"}}
Run Code Online (Sandbox Code Playgroud)
上面是我的代码示例.我正在尝试在我的文件服务器上远程运行此PowerShell代码,并让它返回所有.xls和.xlk文件,其创建日期为2014年6月1日或更晚.当我运行此代码时,它开始吐出该远程位置的所有文件夹.如果我只比较两件事:
PS H:\> Invoke-Command -computername SERVERNAME { Get-ChildItem -path E:\dfsroots\datastore2\public} | Where-Object { $_.extension-match "xls" -and $_.creationtime -ge "06/01/2014"}
Run Code Online (Sandbox Code Playgroud)
仅显示在该日期或之后创建的xls文件.这里发生了什么?我是否需要使用除nest -and和-or语句之外的其他内容?
powershell windows-server-2003 windows-server-2008 windows-7
在大多数情况下,PowerShell使得使用XML非常容易.但是,我在使用"点"表示法来浏览web.config文件时遇到了麻烦.一切正常
Import-Module WebAdministration
$site = get-website | ? { $_.name -eq "My Website - 80" }
$WebConfigFile = $site.physicalPath + "\web.config"
[xml]$WebAppXml = Get-Content $WebConfigFile
$webAppXml.configuration.system.web
Run Code Online (Sandbox Code Playgroud)
在最后一行,PowerShell使用intellisense来填充system.web,但是如果您尝试运行它,则输出为null.我知道我可以使用Xpath或.NET Xml对象,但如果我不需要,我宁愿不这样做.
[Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
New-WebApplication -Name 'testApp' -Site 'Default Web Site' -PhysicalPath c:\test -ApplicationPool DefaultAppPool
Run Code Online (Sandbox Code Playgroud)
这是test.ps1的内容.当我运行.\ test.ps1时出现以下错误.
New-WebApplication:无法检索cmdlet的动态参数.由于以下错误,检索具有CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6}的组件的COM类工厂失败:80040154.在C:\ code\work\users\mchevett\test.ps1:6 char:19 + New-WebApplication <<<< -Name'testApp'-Site'默认网站'-PhysicalPath c:\ test -ApplicationPool DefaultAppPo ol + CategoryInfo:InvalidArgument:(:) [New-WebApplication],ParameterBindingException + FullyQualifiedErrorId:GetDynamicParametersException, Microsoft.IIs.PowerShell.Provider.NewWebApplicationCommand
此错误消息对我没有任何帮助.任何想法如何获得更好的错误消息?谢谢阅读!
我是一个很棒的Console2粉丝,我最近被介绍了PowerShell的强大功能.是否有可能在后者内部使用后者(又称PowerShell解释外壳)?
如何从字符串中提取"程序名称".字符串将如下所示:
%O0033(SUB RAD MSD 50R III)G91G1X-6.4Z-2.F500 G3I6.4Z-8.G3I6.4 G3R3.2X6.4F500 G91G0Z5.G91G1X-10.4 G3I10.4 G3R5.2X10.4 G90G0Z2.M99%
程序名称为(SUB RAD MSD 50R III).将结果存储在另一个字符串中很好.我正在学习powershell,所以任何解释你的答案将不胜感激.
是否有PowerShell命令列出所有以前加载的变量?
我在Visual Studio中运行一些PowerShell脚本,并希望列出当前PowerShell会话中可用的所有变量.
我试过这个命令:
ls variable:*;
Run Code Online (Sandbox Code Playgroud)
但它返回以下内容:
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
Run Code Online (Sandbox Code Playgroud)