powershell cmdlet C#?
本文关键字:cmdlet powershell | 更新日期: 2023-09-27 18:12:08
我累了搜索如何在c#中使用powershell,这是第一次使用powershell,我不知道如何在c#中添加它,我有我的代码在powershell中工作,在c#中添加任何帮助?
New-Item -Path "HKLM:'SOFTWARE'Microsoft'Windows NT'CurrentVersion'Winlogon'SpecialAccounts"
New-Item -Path "HKLM:'SOFTWARE'Microsoft'Windows NT'CurrentVersion'Winlogon'SpecialAccounts'UserList"
New-ItemProperty -path "HKLM:'SOFTWARE'Microsoft'Windows NT'CurrentVersion'Winlogon'SpecialAccounts'UserList" -name BigBear -value "0" -propertyType DWord
(BigBear)它的名字我想用textbox
来改变它i tried this
private void Shell()
{
using (var runspace = RunspaceFactory.CreateRunspace())
{
// using (var powerShell = PowerShell.Create())
// {
// powerShell.Runspace = runspace;
// powerShell.AddScript(@"Hidden.ps1");
// //powerShell.AddParameter("UserName", UserName.Text);
// powerShell.Invoke();
// }
using (var powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
powerShell.AddCommand("New-Item -Path '"HKLM:''SOFTWARE''Microsoft''Windows NT''CurrentVersion''Winlogon''SpecialAccounts'"");
powerShell.AddCommand("New-Item -Path '"HKLM:''SOFTWARE''Microsoft''Windows NT''CurrentVersion''Winlogon''SpecialAccounts''UserList'"");
powerShell.AddCommand("New-ItemProperty -path '"HKLM:''SOFTWARE''Microsoft''Windows NT''CurrentVersion''Winlogon''SpecialAccounts''UserList'" -name " + UserName.Text + " -value '"0'" -propertyType DWord");
//powerShell.AddParameter("ParamA", varA);
var results = powerShell.Invoke();
// Do whatever with results
}
}
你试过了吗?
这里有一些链接,描述了如何做到这一点。但是你的问题没有详细说明你的要求……最后一个有代码,你可以下载和尝试/修改!
https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C