标签: cmdlets

如何在自定义cmdlet中正确使用-verbose和-debug参数

默认情况下,任何具有[CmdletBinding()]属性的命名函数都接受-debug和-verbose(以及其他一些)参数,并具有预定义的$ debug和$ verbose变量.我想弄清楚的是如何将它们传递给在函数内调用的其他cmdlet.

假设我有一个像这样的cmdlet:

function DoStuff() {
   [CmdletBinding()]

   PROCESS {
      new-item Test -type Directory
   }
}
Run Code Online (Sandbox Code Playgroud)

if -debug或者-verbose传入我的函数我想将该标志传递给$debugcmdlet.这样做的正确模式是什么?

parameters powershell cmdlets cmdlet

41
推荐指数
4
解决办法
6万
查看次数

编写PowerShell Cmdlet时如何处理Paths?

在编写C#cmdlet时,将文件作为参数接收的正确方法是什么?到目前为止,我只有一个属性LiteralPath(与其参数命名约定对齐),这是一个字符串.这是一个问题,因为你只需输入控制台中输入的内容; 这可能是完整的路径,也可能是相对路径.

使用Path.GetFullPath(字符串)不起作用.它认为我现在在〜,我不是.如果我将属性从字符串更改为FileInfo,则会出现同样的问题.

编辑:对于任何感兴趣的人,这个解决方法对我有用:

    SessionState ss = new SessionState();
    Directory.SetCurrentDirectory(ss.Path.CurrentFileSystemLocation.Path);

    LiteralPath = Path.GetFullPath(LiteralPath);
Run Code Online (Sandbox Code Playgroud)

LiteralPath是字符串参数.我仍然有兴趣了解处理作为参数传递的文件路径的推荐方法.

编辑2:这是更好的,所以你不要乱用用户当前目录,你应该将其设置回来.

            string current = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(ss.Path.CurrentFileSystemLocation.Path);
            LiteralPath = Path.GetFullPath(LiteralPath);
            Directory.SetCurrentDirectory(current);
Run Code Online (Sandbox Code Playgroud)

c# parameters powershell cmdlets

38
推荐指数
2
解决办法
9767
查看次数

我们能看到PowerShell cmdlet的源代码吗?

我正在学习一些PowerShell.是否可以看到像Get-ChildItem这样的内置cmdlet的源代码?

windows powershell cmdlets

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

如何在调用其他Cmdlet的Cmdlet中支持PowerShell的-WhatIf&-Confirm参数?

我有一个支持-WhatIf&-Confirm参数的PowerShell脚本cmdlet .

它通过$PSCmdlet.ShouldProcess()在执行更改之前调用方法来完成此操作.
这按预期工作.

我遇到的问题是我的Cmdlet是通过调用其他Cmdlet实现的,并且-WhatIf或者-Confirm参数不会传递给我调用的Cmdlet.

我怎么能沿的值传递-WhatIf-Confirm我从我的Cmdlet的调用的cmdlet?

例如,如果我的Cmdlet是Stop-CompanyXyzServices,它用于Stop-Service实现其操作.

如果-WhatIf传递给Stop-CompanyXyzServices我,我希望它也传递给Stop-Service.

这可能吗?

powershell cmdlets powershell-2.0

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

使用PowerShell停止处理,我可以绕过确认吗?

我是一个PowerShell新手,但我经常发现自己在调试一些代码时启动并停止一小组服务.在Powershell中,我可以使用通配符轻松停止进程,但这让我确认.有一个-confirm参数,但我一定不能正确使用它?

`Stop-Process -ProcessName alcore.* -Confirm`
Run Code Online (Sandbox Code Playgroud)

我可以绕过确认并停止流程吗?

谢谢你的帮助,〜在圣地亚哥

powershell cmdlets windows-services

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

无法在PowerShell上导入MSOnline(Connect-MsolService错误)

我有这个问题但找不到任何答案.问题是我试图使用Azure cdmlets通过c#代码连接到O365,但我无法获得connect-msolservice.

当您尝试在Office 365中运行管理Windows PowerShell cmdlet时,"无法识别该术语"错误

windows powershell cmdlets 64-bit azure

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

如何托管Powershell脚本或应用程序,以便可以通过WSManConnectionInfo访问它?(如Office 365)

我知道连接到远程运行空间的唯一方法包括以下参数

   WSManConnectionInfo connectionInfo = 
     new WSManConnectionInfo(false, "localhost", 80, "/Powershell", "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
Run Code Online (Sandbox Code Playgroud)

要么

   WSManConnectionInfo connectionInfo = 
     new WSManConnectionInfo(false, "localhost", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.Powershell", credential);
Run Code Online (Sandbox Code Playgroud)
  • 如何设置我自己的自定义Powershell对象,以便通过HTTP公开它?

  • 使用的正确参数是什么以及如何设置它们?

c# powershell cmdlets powershell-remoting runspace

19
推荐指数
1
解决办法
2867
查看次数

如何找到Azure PowerShell版本?

我需要通过cmdlet代码找到已安装的Azure PowerShell版本.如何找到Azure PowerShell版本?

注意:除了cmdlet代码之外,也欢迎使用.

powershell cmdlets version azure azure-powershell

19
推荐指数
4
解决办法
3万
查看次数

如何将PowerShell cmdlet或函数添加到我的计算机以便始终可用?

如果我找到(或创建)新的PowerShell cmdlet(或函数),如何将其添加到我的计算机?

  • 我将它复制到特定文件夹吗?
  • 我是否将其内容放在特定文件中?
  • 我是需要授权,签名还是以某种方式授予权限?

我不想在一个会话中使用它; 我希望每当我在这台机器上使用PowerShell时它都可用.

profile powershell cmdlets

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

你最喜欢的Powershell Cmdlet是什么?

我刚刚发现/ n软件免费使用Powershell NetCmdlets,在使用它们之后,我喜欢它们带给命令行的功能.所以它提出了一个问题,你最喜欢的Cmdlet是什么,你如何使用它们?

powershell cmdlets

15
推荐指数
2
解决办法
2038
查看次数