我需要创建一个新的本地用户帐户,然后将它们添加到本地Administrators组.可以在PowerShell中完成吗?
编辑:
# Create new local Admin user for script purposes
$Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer"
$LocalAdmin = $Computer.Create("User", "LocalAdmin")
$LocalAdmin.SetPassword("Password01")
$LocalAdmin.SetInfo()
$LocalAdmin.FullName = "Local Admin by Powershell"
$LocalAdmin.SetInfo()
$LocalAdmin.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
$LocalAdmin.SetInfo()
Run Code Online (Sandbox Code Playgroud)
我有这个,但想知道是否还有更多的PowerShell风格.
我正在寻找一种方法来继续在脚本中调用重启后停止的Powershell脚本.例如,我通过Powershell自动化构建DC,在将PC重命名为TESTDC01之后,需要重新启动,但在重新启动后,继续使用脚本继续执行dcpromo等.
这可能吗?
干杯!
首先,在Win7/Win8等上执行代码没有问题.该问题仅存在于Windows XP上.代码在一个按钮中,基本上运行taskmgr.exe作为另一个用户凭据(本地管理员凭据,这是一个Kiosk PC,它加载一个C#应用程序,可以登录然后加载cmd.exe/taskmgr.exe作为本地管理员不受链接到Kiosk用户的GPO的影响.
但是,在XP上,单击按钮时出现错误:Stub收到错误数据.
码:
private void btnTaskMgr_Click(object sender, EventArgs e)
{
string password = "myPassword";
SecureString secureString = new SecureString();
foreach (char chr in password) secureString.AppendChar(chr);
ProcessStartInfo processAdmin;
processAdmin = new ProcessStartInfo();
processAdmin.UseShellExecute = false;
processAdmin.Password = secureString;
processAdmin.UserName = "admin";
processAdmin.FileName = "taskmgr.exe";
processAdmin.WorkingDirectory = "C:\\Windows\\System32";
Process.Start(processAdmin);
}
Run Code Online (Sandbox Code Playgroud)

与在C和其他语言中一样,您可以在物理执行脚本时为脚本/ .exe参数指定.例如:
myScript.exe Hello 10 P
其中Hello,10和P被传递给程序本身的某个变量.
在Powershell中这可能吗?如果是这样,你如何将这些args赋予给定的$变量
谢谢!
这是我想的最佳实践问题.
在设计将在脚本中使用的函数时,处理函数内可能发生的错误的最佳方法是什么?
例如,假设我们有一个执行X和Y的基本功能:
Function Test-Function
{
Try
{
<# Something in here that would generate an error #>
}
Catch
{
Throw
}
Return $someReturnResultIWantInMyScript
}
Run Code Online (Sandbox Code Playgroud)
我的脚本调用此函数:
Try
{
$ValueIWantFromFunction = Test-Function
}
Catch
{
<# Do something here #>
}
Run Code Online (Sandbox Code Playgroud)
如果Test-Function遇到终止错误,它将抛给调用者.将Try/Catch围绕在我的脚本函数调用将收到此错误并击中了自己的渔获物.然后我可以决定做什么.
如果我没有在函数中抛出错误,脚本将看不到终止错误,然后我$ValueIWantFromFunction可能包含$Null或无用的东西.
这是在脚本中使用函数和函数调用进行错误处理的好方法吗?有没有更好的办法?
就realloc的使用而言,它确实是一个建议,更具体地说,如果我可以利用它来简化我现有的代码.基本上,下面的内容,它动态分配一些内存,如果我超过256,那么数组需要增加大小,所以我malloc一个临时数组,大小2倍,memcpy等(见下文).
我只是想知道是否可以在下面的代码中使用realloc,简化它,任何建议,示例代码,甚至是如何实现它的提示非常感谢!
干杯.
void reverse(char *s) {
char p;
switch(toupper(s[0]))
{
case 'A': case 'E': case 'I': case 'O': case 'U':
p = s[strlen(s)-1];
while( p >= s )
putchar( p-- );
putchar( '\n' );
break;
default:
printf("%s", s);
break;
}
printf("\n");
}
int main(void) {
char c;
int buffer_size = 256;
char *buffer, *temp;
int i=0;
buffer = (char*)malloc(buffer_size);
while (c=getchar(), c!=' ' && c!='\n' && c !='\t')
{
buffer[i++] = c;
if ( i >= buffer_size )
{ …Run Code Online (Sandbox Code Playgroud) 我需要计算文本文件中的行数,并将其用作循环的循环变量for.问题是:
$lines = Get-Content -Path PostBackupCheck-Textfile.txt | Measure-Object -Line
Run Code Online (Sandbox Code Playgroud)
虽然这确实返回了行数,但它返回的状态无法与循环中的整数进行比较:
for ($i=0; $i -le $lines; $i++)
{Write-Host "Line"}
Run Code Online (Sandbox Code Playgroud) 是否可以搜索通配符 - 示例*WAAgent*或*WAHost*删除引用上述通配符语句的每个注册表项?
这可能吗?
我想我们需要以某种方式调用WUAgent来运行检测,但我想基本上下载并安装更新,然后重新启动作为脚本的一部分.
这将是一个更大的脚本的一部分,通过Powershell基本上构建一个vanilla 2008R2盒子直到DC.
所以我有以下代码输出安装的所有功能和角色:
Import-Module ServerManager
$Arr = Get-WindowsFeature | Where-Object {$_.Installed -match “True”} | Select-Object -Property Name
$loopCount = $Arr.Count
For($i=0; $i -le $loopCount; $i++) {
Write-Host $Arr[$i]
}
Run Code Online (Sandbox Code Playgroud)
但是,输出是:
@{Name=Backup-Features}
@{Name=Backup}
@{Name=Backup-Tools}
Run Code Online (Sandbox Code Playgroud)
我怎么能摆脱@和{}?