在PowerShell 1.0中,如果我有一个枚举类型的cmdlet参数,那么测试用户是否在cmdlet命令行上指定该参数的推荐方法是什么?例如:
MyEnum : int { No = 0, Yes = 1, MaybeSo = 2 }
class DoSomethingCommand : PSCmdlet
...
private MyEnum isEnabled;
[Parameter(Mandatory = false)]
public MyEnum IsEnabled
{
get { return isEnabled; }
set { isEnabled = value; }
}
protected override void ProcessRecord()
{
// How do I know if the user passed -IsEnabled <value> to the cmdlet?
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,而不必使用虚拟值种子isEnabled?默认情况下,它将等于0,我不希望为每个参数设置种子或为我的枚举添加虚拟值.我可能有很多带有100个参数的cmdlet,必须有更好的方法.这与这个问题有关,但我一直在寻找一种更清洁的方法.谢谢.
我有两个返回对象列表的CMDlet.一个返回SPSolution类型的对象,其中包含属性Id,另一个返回SPFeature类型的对象,其属性为SolutionId.
现在我想加入/合并这样的数据:
$f = Get-Feature
$s = Get-Solution
$result = <JOIN> $f $s
<ON> $f.SolutionId = $s.Id
<SELECT> FeatureName = $f.DisplayName, SolutionName = $s.Name
Run Code Online (Sandbox Code Playgroud) 有没有办法更改High Impact PowerShell脚本的默认确认选项?
当我实现一个Cmdlet并运行它要求确认时
MyPS
Confirm
Are you sure you want to perform this action?
Performing operation "XYZ" on Target "123".
[Y] Yes [A] Yes to All [N] No [L] No to all [S] Suspend [?] Help (default is "Y"):
Run Code Online (Sandbox Code Playgroud)
如何更改默认值?我想将默认值从"Y"更改为"N".
我正在使用Start-AzureWebsite(以及Stop-AzureWebsite)azure powershell cmdlet来启动azure网站.它工作了大约3个月,并在2天前(2014年1月31日)停止工作(没有任何环境变化).现在两个cmdlet都崩溃了,错误如下:
C:\> Start-AzureWebsite -Name mywebsite
Start-AzureWebsite : String was not recognized as a valid Boolean.
At line:1 char:1
+ Start-AzureWebsite -Name mywebsite
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Start-AzureWebsite], FormatException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Websites.StartAzureWebsiteCommand
Run Code Online (Sandbox Code Playgroud)
我试图在不同的帐户和不同的机器上运行它但没有成功.我还尝试安装最新版本的azure sdk.
如果您对此问题有任何建议,请与我们联系.
-Petro
我正在尝试为PowerShell管理单元创建自己的cmdlet集.我遇到的问题是我创建了自己的对象,我在ProcessRecord方法中创建和填充但我无法更改返回类型以允许我返回我创建的对象.
protected override void ProcessRecord()
{
ReportFileSettings rptFileSettings = new ReportFileSettings();
rptFileSettings.Enabled = string.Equals((reader.GetAttribute("Enabled").ToString().ToLower()), "yes");
rptFileSettings.FileLocation = reader.GetAttribute("FileLocation").ToString();
rptFileSettings.OverwriteExisting = string.Equals(reader.GetAttribute("OverwriteExistingFile").ToString().ToLower(), "yes");
rptFileSettings.NoOfDaysToKeep = int.Parse(reader.GetAttribute("NumberOfDaysToKeep").ToString());
rptFileSettings.ArchiveFileLocation = reader.GetAttribute("ArchiveFileLocation").ToString();
return rptFileSettings;
}
Run Code Online (Sandbox Code Playgroud)
这是我的ProcessRecord方法,但是因为它覆盖了PSCmdlet中的那个,所以无法从void更改返回类型.
任何人都可以帮助返回rptFileSettings对象的最佳方法,以便我可以将其与其他cmdlet中的值一起使用吗?
我试图执行一些简单的if语句,但所有基于[Microsoft.Management.Infrastructure.CimInstance]的新cmdlet似乎都没有公开.count方法?
$Disks = Get-Disk
$Disks.Count
Run Code Online (Sandbox Code Playgroud)
不归还任何东西.我发现我可以将它转换为[数组],这使得它返回一个.NET .count方法,如预期的那样.
[Array]$Disks = Get-Disk
$Disks.Count
Run Code Online (Sandbox Code Playgroud)
这可以直接将其作为以前cmdlet的数组投射:
(Get-Services).Count
Run Code Online (Sandbox Code Playgroud)
推荐的解决方法是什么?
一个不起作用的例子:
$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
Else If ($PageDisk.Count -eq 1) { Do X }
Run Code Online (Sandbox Code Playgroud)
选项A(演员阵容):
[Array]$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue} …Run Code Online (Sandbox Code Playgroud) 我正在 powershell 上编写一个 cmdlet(脚本),我想使用 eunm 作为参数之一。但我不知道将枚举定义放在哪里,以便它对 cmdlet 参数声明可见。
例如,我有一个像这样的脚本的参数定义
[cmdletbinding()]
param(
[Parameter(Mandatory=$True)]
[string]$Level
)
Run Code Online (Sandbox Code Playgroud)
和这样的枚举
enum LevelEnum { NC = 1; NML = 2; CS = 3 }
Run Code Online (Sandbox Code Playgroud)
我无法替换参数定义中的[string]with [LevelEnum],因为脚本将无法找到枚举定义。而且我之前不能放定义cmdletbinding,这是不允许的。如果那是一个函数,我知道该怎么做,我知道它可以使用 解决ValidateSet,但我需要有与枚举选项相关的整数值。
[ValidateSet('NC','NML','CS')]
Run Code Online (Sandbox Code Playgroud)
但问题是,我可以对 cmdlet 做同样的事情吗?
谢谢大家。我最终得到了不同答案的组合。
[cmdletbinding()]
param(
[Parameter(Mandatory=$True)]
[ValidateSet('NC','NML','CS')]
[string]$Level
)
# Convert level from string to enum
enum PatchLevel { NC = 1; NML = 2; CS = 3 }
[PatchLevel]$l = $Level
# Use the numeric value
Write-Host $l.value__
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Windows Server 2008 R2 VM 上使用 PowerShell 创建 IIS 应用程序和应用程序池。powershell脚本如下:
Param(
[string] $branchName,
[string] $sourceFolder
)
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You do not have Administrator rights to run this script. `nPlease re-run this script as an Administrator."
Exit
}
$appPool = $branchName
$site = "Default Web Site"
#Add APPPool
New-WebAppPool -Name $appPool -Force
#Create Applications
New-WebApplication -Name $branchName -Site $site -PhysicalPath $sourceFolder - ApplicationPool $appPool -Force
Run Code Online (Sandbox Code Playgroud)
如果我在 PowerShell ISE 中运行脚本,它运行良好,但如果我从命令行(或使用命令行的批处理文件)运行它,我会收到错误
术语 New-WebAppPool 不被识别为 cmdlet 的名称...等。 …
我正在尝试Get-AzureRmEventHubNamespaceKey在 Octopus 中的 Azure Powershell 步骤中运行cmdlet。
我收到以下错误:
Get-AzureRmEventHubNamespaceKey : The Azure PowerShell session has not been properly
initialized. Please import the module and try again
Run Code Online (Sandbox Code Playgroud)
该模块安装在八达通服务器的以下目录中:
C:\Program Files (x86)\Microsoft
SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.EventHub
作为同一步骤的一部分,我尝试先导入模块:
Import-Module –Name "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.EventHub" -Verbose
我可以在输出中看到它已被导入:
VERBOSE: Importing cmdlet 'Get-AzureRmEventHubNamespaceKey'.
但紧随其后的是上述错误。如果我 RDP 到章鱼服务器并直接从那里运行它运行良好。
关于可能导致这种情况的任何想法?
为高级函数编写xml帮助文件的资源似乎非常有限.我希望使用基于xml的帮助文件,但似乎这需要我每个cmdlet有一个xml文件,这是一个庞大的xml文件.每个cmdlet都使用.ExternalHelp为其分配xmlfile.
有没有办法将许多cmdlet的帮助放入一个文件中,然后将每个cmdlet正确指向文件的正确部分?
cmdlets ×10
powershell ×10
azure ×2
c# ×2
.net ×1
arrays ×1
batch-file ×1
cim ×1
cmdlet ×1
iis ×1
join ×1
parameters ×1
pscmdlet ×1
return-type ×1
scalar ×1
xml ×1