我正在编写一个 Powershell 脚本来执行完整的 Windows 7 构建。我有一个添加所需用户的功能,但它没有设置主目录,这导致脚本的后续部分(设置用户桌面)失败,因为目录“尚不存在”。
以下是该函数的主要部分:
[CmdletBinding()]
Param(
[Parameter(Position=0,
Mandatory=$True,
ValueFromPipeline=$True)]
[string]$userName,
[Parameter(Position=1,
Mandatory=$True,
ValueFromPipeline=$True)]
[string]$password,
[string]$computerName = $env:ComputerName,
[string]$description = "Created by PowerShell"
)
$computer = [ADSI]"WinNT://$computerName"
$user = $computer.Create("User", $userName)
$user.setpassword($password)
$user.put("description",$description)
$user.SetInfo()
} #end function New-LocalUser
有没有地方可以添加类似的东西$用户.put(“HomePath”,“\Users\automotive\Desktop”)例如这样它就不会破坏我脚本的后面部分。
这是我在 Users.cmd 脚本中使用的代码:
这是我的 .cmd 文件中的代码:
SETLOCAL
SET Profile=C:\Users
SET user=cars
SET password=cars
SET PSEXEC="C:\Program Files\SysinternalsSuite\PsExec.exe"
SET wmic=%windir%\System32\Wbem\WMIC
FOR /F "tokens=2* delims= " %%I IN ('REG QUERY ^"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList^" /v ProfilesDirectory^|find /i ^"ProfilesDirectory^"') DO (SET ProfilesDirectory=%%J)
@echo ProfilesDirectory: %ProfilesDirectory%
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory /t REG_SZ /d "%Profile%" /f
REG ADD "HKCU\Software\Systinternals\PsExec" /v EulaAccepted /t REG_DWORD /d 1 /f
NET USER /add %user% %password% /comment:"Account for User" /expires:never /homedir:%Profile%\%user%
NET LOCALGROUP Administrators /add %user%
%PSEXEC% -h -u %user% -p %password% "%wmic% QUIT"
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory /t REG_SZ /d "%ProfilesDirectory%" /f
我使用此命令调用它,然后按照我的命令来创建新的桌面。
\Windows\System32\cmd /c c:\Users\Administrator\Desktop\Users.cmd -wait
Remove-Item \Users\cars\Desktop -recurse -force
Copy-Item E:\"Desktop Cars" C:\Users\cars Desktop -recurse
DOS 命令运行良好,直到
%PSEXEC% -h -u %user% -p %password% "%wmic% QUIT"
接下来是红色的所有内容,然后才是最后的 REG ADD 命令 -
C:\>"C:\Program Files\SysinternalsSuite\PsExec.exe" -h -u cars -p cars "C:\Windows\System32\Wbem\WMIC QUIT"
cmd.exe :
At C:\Users\Administrator\Desktop\Desktops.ps1:3 char:22
+ \Windows\System32\cmd <<<< /c c:\Users\Administrator\Desktop\Users.cmd -wait
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
The system cannot find the file specified.
Connecting to local system...Starting PsExec service on local system...Connecting with PsExec service on ccm2756...Starting C:\Windows\
System32\Wbem\WMIC QUIT on ccm2756...
PsExec could not start C:\Windows\System32\Wbem\WMIC QUIT on ccm2756:
之后,它添加了帐户和更改桌面工作的命令,但是用户桌面的文件处于“正在使用”状态,直到我重新启动系统。
答案1
添加用户并在本地设置主目录:
NET USER UserName DerParol /add /comment:"Account for User" /expires:never /homedir:C\Acc\UserName
在本地管理员组添加用户。
NET LOCALGROUP Administrators /add UserName
PSexec
部分Sysinternals 套件
创建用户个人资料,
proinit.cmd:
@ECHO OFF
SETLOCAL
SET Profile=C:\Acc
SET user=Troll
SET password=DerParol
SET PSEXEC="C:\Program Files\SysinternalsSuite\PSEXEC"
SET wmic=%windir%\system32\wbem\wmic
FOR /F "tokens=2* delims= " %%I IN ('REG QUERY ^"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList^" /v ProfilesDirectory^|find /i ^"ProfilesDirectory^"') DO (
SET ProfilesDirectory=%%J)
@echo ProfilesDirectory: %ProfilesDirectory%
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory /t REG_SZ /d "%Profile%" /f
REG ADD "HKCU\Software\Sysinternals\PsExec" /v EulaAccepted /t REG_DWORD /d 1 /f
NET USER /add %user% %password% /comment:"Account for User" /expires:never /homedir:%Profile%\%user% /profilepath:%Profile%\%user%
NET LOCALGROUP Administrators /add %user%
%PSEXEC% -h -u %user% -p %password% "%wmic% QUIT"
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory /t REG_SZ /d "%ProfilesDirectory%" /f
域名,用途:dsadd user
@ECHO OFF
@SET user=UserName
@SET password=DerParole
@SET pwdSet=-canchpwd yes -pwdneverexpires yes
::-mustchpwd yes
@SET accSet=-disabled no -acctexpires never -upn %user%@%USERDNSDOMAIN% -samid %user%
@SET dirSet=-profile \\SERV\Acc\%user% -hmdrv R: -hmdir \\SERV\Acc\%user% -loscr enver.cmd
@SET Brand=-office "Grand-1" -company TT -dept HT -title MegaUser
@SET locSet=-tel 914-55-04-42 -email %user%II@%USERDNSDOMAIN% -webpg %user%.%USERDNSDOMAIN%
@SET callIn=-hometel 9-slip-010-292 -pager 1022 -mobile "8(777) 101-44-22" -fax "977-101-44-22" -iptel 6002
@SET nameSt=-fn User -ln Name -mi "U.N." -display "UserName II"
@SET Description=-desc "Account for User"
::@SET cmSet=@ECHO dsadd user
@SET cmSet=dsadd user
@For /F %%a In ('dsquery * -filter "(objectCategory=domain)"') Do @SET dc=%%a
@SET dn="cn=%user%,cn=users,%dc:~1%
::@SET grpSet=-memberof "cn=ResourceGroup,cn=users,%dc:~1%
%cmSet% -pwd %password% %dn% %pwdSet% %accSet% %dirSet% %Brand% %locSet% %callIn% %nameSt% %Description% -uc
dsquery * -filter "(samAccountName=%user%)" -attr * | more
dsadd用户测试批处理脚本: