我在windows powershell中创建了一个create函数:
function create(){
param(
$searchBase = "OU=Customers,DC=test,DC=nl",
$NewOUs = @(Import-csv -Path $txt_csv.Text -Delimiter ";"),
[switch]$ProtectOU
)
$Protect = $true
If ($ProtectOU){$Protect = $true}
<# ------- CREATE OU ------- #>
foreach ($NewOU in $NewOUs) {
try {
New-ADOrganizationalUnit -Name $NewOU.company -Description $NewOU.description -Path $searchBase -ProtectedFromAccidentalDeletion $Protect
}
catch {
Write-Host "OU already exists"
}
}
$UserList = Import-Csv -Path $txt_csv.Text -Delimiter ";"
<# ------- CREATE USERS ------- #>
foreach ($User in $UserList) {
$OU = $User.path
$UPN = $User.UPN
$Password = $User.password
$Detailedname = $User.firstname + " " + $User.Lastname
$UserFirstname = $User.Firstname
$FirstLetterFirstname = $UserFirstname.substring(0,1)
$SAM = $User.UPN
$Company = $User.company
$Description = $User.description
$AccountExpirationDate = $User.accountexpirationdate
$params = @{ 'Name'=$Detailedname;
'SamAccountName'=$SAM;
'UserPrincipalName'=$UPN+'@test.nl';
'DisplayName'=$Detailedname;
'GivenName'=$UserFirstname;
'Surname'=$User.Lastname;
'AccountPassword'=(ConvertTo-SecureString $Password -AsPlainText -Force);
'Enabled'=$True;
'PasswordNeverExpires'=$True;
'Path'=$OU;
'Company'=$Company;
'Description'=$Description;
'AccountExpirationDate'=$AccountExpirationDate
'HomeDrive' = "H:"
'HomeDirectory' = "\\home\userdata$\$SAM"
'ProfilePath'="\\dc-test\User Profiles$\$SAM"
}
New-ADUser @params
}
我希望这个创建函数作为输出文件,例如在txt中。
这是我的wpf按钮对象:
$button_add.Add_Click({create})
当我点击按钮时,输出必须生成一个输出文件。我已经尝试了很多解决方案,例如:
创建|外部文件,但我没有得到我想要的信息:
来源:https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/out-file
是否可以这样做?
亲切的问候
答案 0 :(得分:0)
您需要执行以下操作:
"processing New-ADUser with $params" | Out-File log.txt -Append
try {
New-ADUser @params
"Created $UPN" | Out-File log.txt -Append
}
catch {
$_ | Out-File log.txt -Append
}
或者您可以将New-ADUser的输出传递给Out-File:
New-ADUser @params -PassThru | Out-File log.txt -Append