我在.net中实现了客户cmdlet.我想知道用户传递给它的所有参数.
My-Cmdlet -foo -bar -foobar
Run Code Online (Sandbox Code Playgroud)
基本上我想知道用户以编程方式使用参数foo,bar,foobar执行此cmdlet.
在脚本中看起来我们可以使用:$ PSBoundParameters.ContainsKey('WhatIf')
我需要.net(c#)中的那个.
我已经看到了一些类似的问题,但仍然无法弄清楚如何解决它:我有一个简单的PowerShell cmdlet(只是添加参数)和一个snapin来注册它.问题是我尝试运行installutil我得到
No public installers with the RunInstallerAttribute.Yes attribute could be found
Run Code Online (Sandbox Code Playgroud)
snapin代码:
using System.ComponentModel;
using System.Management.Automation;
namespace Cmdtlets
{
[RunInstaller(true)]
class BugBoxSnapin : PSSnapIn
{
}
}
Run Code Online (Sandbox Code Playgroud)
我知道有64个32位版本的installutil以及x86和x64 powershells.我的配置是 - 64位机器,平台目标"任何CPU",.NET Framework 4.5.我无法从visual studio引用选择器引用System.Management.Automation,所以我手动更改了csproj文件
<Reference Include="System.Management.Automation" />
Run Code Online (Sandbox Code Playgroud)
我应该在哪个版本的powershell中运行哪个版本的installutil?(我想我尝试了所有组合,但仍然遇到同样的问题).
还有一个问题:在这个工作之后,我将需要与32位COM对象进行交互,我应该如何设置项目?
有没有办法通过使用PowerShell来获取账单信息(时间和金钱花费)的特定资源组?
使用链接跟随导出SQL数据库示例:
得到以下错误:
New-AzureRmSqlDatabaseExport:ResourceNotFound:找不到资源组"Default-SQL-SoutheastAsia"下的资源"Microsoft.Sql/servers/XXX.database.windows.net/databases/[DBNAME]".在[FilePath]\sample.ps1:24 char:18 + $ exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ ResourceGroupN ... + ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 〜+ CategoryInfo:CloseError:(:) [New-AzureRmSqlDatabaseExport],CloudException + FullyQualifiedErrorId:Microsoft.Azure.Commands.Sql.ImportExport.Cmdlet.NewAzureSqlDatabaseExport
Get-AzureRmSqlDatabaseImportExportStatus:无法将参数绑定到参数'OperationStatusLink',因为它为null.在[FilePath]\sample.ps1:30 char:63 + Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $ exportRequest.Ope ... + CategoryInfo:InvalidData :( :) [Get-AzureRmSqlDatabaseImportExportStatus],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure. Commands.Sql.ImportExport.Cmdlet.GetAzureSqlDatabaseImportExportStatus
有帮助吗?
我正在编写Get-EventLog cmdlet的图形实例,以便我可以锁定用户可以输入的参数和值.在下图中,紫色框(左列)将由我设置,但棕褐色框(右列)可以接受任何值的用户输入.
在后端,我正在编写一个函数,它将采用用户输入的任何值,并使用这些值调用Get-EventLog cmdlet.例如,如果用户只输入最新参数的值,那么这将是它将生成的代码:
Get-EventLog -LogName Application -Source sourcename -Newest uservalue `
-After $null -Before $null
Run Code Online (Sandbox Code Playgroud)
问题是cmdlet无法识别和参数$null的可接受输入.如何在不抛出错误的情况下将空值传递给cmdlet的参数?-After-Before
我正在运行一个PS命令get-Keyproperty,它以下面的表格格式返回结果"
Key Label Policy Running Required
--- ----- ------ ------- --------
abc UI on True False
efg UI off True False
Run Code Online (Sandbox Code Playgroud)
我想检索Policy其中的值on
如何检索Policy价值abc Key?
我正在跑,get-Keyproperty | Select-Object abc但它不起作用.
我在 c# 中创建了一个简单的 PowerShell 模块,它实现了一些 cmdlet,我希望能够在导入模块时运行代码。
从在 google 和命名空间中环顾四周,似乎没有这样做的正确方法。
到目前为止,我提出的解决方法是创建一个 psm1 或 ps1 文件,该文件在加载模块时运行并执行启动操作(宁愿不使用它,因为脚本在某些将运行的环境中被阻止)。
其他选项是我已经能够通过创建一个 CmdletProvider 来做到这一点,但它在使用 new-psdrive 时会在提供程序列表中创建一个垃圾条目。
[CmdletProvider("junkprovider", ProviderCapabilities.None)]
public class Startup : CmdletProvider
{
Public Startup()
{
// Startup code here
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法正确地做到这一点,或者我将不得不使用黑客?
无论如何,PowerShell是否可以在输出到文件之前先输出不含ANSI控制字符(例如颜色控制,ex [1;xxm或)[xm]的文件,
[1;35mStarting selenium server... [0m[1;35mstarted - PID: [0m 22860
[0;36m[Signin Test] Test Suite[0m
[0;35m================================[0m
Running: [0;32mstep 1 - launch the browser[0m
[1;35m[40mINFO[0m [1;36mRequest: POST /wd/hub/session[0m
Run Code Online (Sandbox Code Playgroud)
输出在PowerShell终端中以颜色正确显示(我用过chcp,不起作用)
我创建了一个非常简单的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)
我的脚本有什么问题?
cmdlets ×9
powershell ×9
c# ×3
azure ×2
cmdlet ×2
export ×1
installutil ×1
null ×1
powercli ×1
unicode ×1