Tag: PowerShell V2.0的

如何在运行时通过脚本检查PowerShell中是否存在cmdlet

我有一个PowerShell脚本需要在多个主机(PowerGUI, PowerShell ISE等)下运行,但是我有一个问题,其中一个主机下有时不存在cmdlet。 有没有办法检查是否存在一个cmdlet,以便我可以将代码封装在if块中,并在不存在时执行其他操作? 我知道我可以使用$host.name来$host.name假设在每个主机上运行的代码,但是我希望使用function检测来代替将来添加的cmdlet。 我也可以使用一个try / catch块,但是由于它运行在托pipe代码中,我假设有一段距离来检测是否通过代码安装了cmdlet。

使用Start-Process和WaitForExit而不是-Wait获取ExitCode

我试图从PowerShell运行一个程序,等待退出,然后访问ExitCode,但没有太多的运气。 我不想用-Wait和Start-Process,因为我需要在后台进行一些处理。 这是一个简化的testing脚本: cd "C:\Windows" # ExitCode is available when using -Wait… Write-Host "Starting Notepad with -Wait – return code will be available" $process = (Start-Process -FilePath "notepad.exe" -PassThru -Wait) Write-Host "Process finished with return code: " $process.ExitCode # ExitCode is not available when waiting separately Write-Host "Starting Notepad without -Wait – return code will […]

从另一个powershell脚本加载variables

我有几个脚本可以重用variables,所以我想在他们自己的Variables.ps1脚本中隔离variables,即 $var1 = "1" $var2 = "2" 我试图加载这些variables,然后在Main.ps1脚本中打印出来,如下所示: .\Variables.ps1 $var1 $var2 这工作如果我第一次运行。\ Variables.ps1但不是如果我只是运行Main.ps1。 我的环境是PowerShell ISE。 我究竟做错了什么?

如何获取帮助信息以显示我的Powershell脚本参数?

我有一个setup.ps1脚本( setup.ps1 ),我们用它作为开发环境设置脚本的入口点。 它需要一个参数: param( [Parameter(Position=0,HelpMessage="The targets to run.")] [Alias("t")] [string[]] $Targets = "Help" ) 当我跑步 PS > get-help .\setup.ps1 -detailed 在参数部分,我的帮助信息不会出现: PARAMETERS -Targets <String[]> 我需要做什么才能让我的参数帮助信息显示?

在PowerShell中使用的GetType,variables之间的区别

variables$a和$b什么区别? $a = (Get-Date).DayOfWeek $b = Get-Date | Select-Object DayOfWeek 我试图检查 $a.GetType $b.GetType MemberType : Method OverloadDefinitions : {type GetType()} TypeNameOfValue : System.Management.Automation.PSMethod Value : type GetType() Name : GetType IsInstance : True MemberType : Method OverloadDefinitions : {type GetType()} TypeNameOfValue : System.Management.Automation.PSMethod Value : type GetType() Name : GetType IsInstance : True 但是,尽pipe这些variables的输出看起来不同,但似乎没有区别。

我可以用C#和Powershell做什么?

我对C#有一个很好的理解,并对PowerShell有一个非常基本的理解。 我正在使用Windows PowerShell CTP 3,这非常有趣。 但我想超越编写脚本/函数。 C#有没有很酷的东西?

Powershell 2副本项目创build一个文件夹如果不存在

$from = "\\something\1 XLS\2010_04_22\*" $to = "c:\out\1 XLS\2010_04_22\" copy-item $from $to -Recurse 如果c:\ out \ 1 XLS \ 2010_04_22 \确实存在,这将起作用。 如果不存在,是否可以使用单个命令创build“c:\ out \ 1 XLS \ 2010_04_22 \”。

如何从模块中检索可用的命令?

要知道哪台PowerShell模块可以在一台机器上使用该命令 Get-Module -ListAvailable 这将返回一个带有模块types,名称和导出命令的列表。 但导出的命令总是空的,只是显示{}为什么不显示? 我必须使用另一个参数还是有另一个cmdlet或方法来检索可用的命令?

PowerShell:创build本地用户帐户

我需要创build一个新的本地用户帐户,然后将其添加到本地pipe理员组。 这可以在PowerShell中完成吗? 编辑: # Create new local Admin user for script purposes $Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer" $LocalAdmin = $Computer.Create("User", "LocalAdmin") $LocalAdmin.SetPassword("Password01") $LocalAdmin.SetInfo() $LocalAdmin.FullName = "Local Admin by Powershell" $LocalAdmin.SetInfo() $LocalAdmin.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD $LocalAdmin.SetInfo() 我有这个,但是想知道是否还有更多的PowerShell-esque。

如何正确地过滤PowerShell复制脚本中的多个string

我从这个答案使用PowerShell脚本做文件复制。 当我想要使用filter包含多个文件types时出现问题。 Get-ChildItem $originalPath -filter "*.htm" | ` foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` New-Item -ItemType File -Path $targetFile -Force; ` Copy-Item $_.FullName -destination $targetFile } 像梦一样工作。 但是,当我想要使用filter包含多个文件types时出现问题。 Get-ChildItem $originalPath ` -filter "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*") | ` foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` New-Item -ItemType File -Path $targetFile -Force; ` Copy-Item $_.FullName -destination $targetFile } […]