在调用 Powershell Cmdlets 之间保持状态的最佳做法是什么? [英] What is the best practice for keeping state between calls to Powershell Cmdlets?

查看:38
本文介绍了在调用 Powershell Cmdlets 之间保持状态的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全是 Powershell 开发的新手,我正在尝试用 C# 编写一个 Powershell Cmdlet,该 Cmdlet 将用作 REST API 的接口.

I am a complete newbie to Powershell development and I am trying to write a Powershell Cmdlet in C# that will be used as an interface to a REST API.

我想要某种设置 Cmdlet,其中将提示用户输入 REST API 的 Uri、用户名和密码,然后调用 Get-Item 等 Cmldlet,而无需输入这些参数.与 Powershell 的 Azure 提供程序的工作原理非常相似,您可以通过调用 Select-AzureSubscription 设置当前订阅,然后调用 Save-AzureVhd,而无需再次输入订阅名称.

I would like to have some kind of setup Cmdlet where the user will be prompted for an Uri, username and password to the REST API and then call Cmldlets like Get-Item without having to enter those parameters. Pretty much like the Azure provider for Powershell works where you can set the current subscription by calling Select-AzureSubscription and then call Save-AzureVhd without having to enter the subscription name again.

在调用不同 Cmdlet 之间保持状态的最佳做法是什么?

What is best practices for keeping state between calls to different Cmdlets?

我不确定这是解决它的最佳方法,但我所做的是添加了一个保存状态的单例类.我有一个 Cmdlet,Select-Project -Name MyProject,它在我的单例类中设置了一个公共属性,然后我的其他 Cmdlet 可以访问该属性.

I am not sure that this is the best way to solve it but what I did is that i added a singleton class that holds the state. I have one Cmdlet, Select-Project -Name MyProject that sets a public property in my singleton class and then my other Cmdlets can access that property.

推荐答案

如果他们运行的是 V3 或更高版本,您可以让安装程序在 $PSDefaultParameterValues 中设置这些值.

If they're running V3 or better, you could have the setup set those values in $PSDefaultParameterValues.

见:

get-help about_parameters_default_values

有关设置值的详细信息.

for details on setting values.

这篇关于在调用 Powershell Cmdlets 之间保持状态的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆