Answered by:
Current script must identify the user who ran it

Question
-
I am fairly new at Powershell and want to be able to find out who ran the script
this is my code so far
param(
[Parameter(Mandatory=$true)]$csvfile
)
Import-Module activedirectory
$users = Import-Csv -Path $csvfile
foreach ($User in $users)
{ $Displayname = $User.lastname + "," + $User.Firstname
$GivenName = $User.Firstname
$Sn = $User.Lastname
$Name = "$($User.Lastname), $($User.Firstname)"
$SamAccountName = "$($User.Firstname).$($User.Lastname)"
$UserPrincipalName = $UserPrincipalName = $User.Firstname + "." + $User.Lastname + "@test.ruh.local"
$ProfilePath = $User.OU
$Description = $User.Description
$password = $User.Password1
#creates new users
New-ADUser -DisplayName $Name -givenname "$GivenName" -Surname "$SN" -Name "$Name" -SamAccountName "$SamAccountName" -UserPrincipalName "$UserPrincipalName" -ProfilePath "$ProfilePath" -Description "$Description" -Enabled $False
Add-ADGroupMember -identity Guests -Members $SamAccountName #adds the groups
Add-ADGroupMember -identity Users -Members $SamAccountName
Write-Output "$(Get-Date) $SamAccountName" | Out-file C:\temp\test25.txt -appendI would ideally like to add it to the current out-file
please could someone advise how I do this
Wednesday, April 10, 2019 11:28 AM
Answers
-
You'll need to add a Try/Catch block around the code that might generate an error.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6
- Marked as answer by Jon Gomes Thursday, April 11, 2019 8:09 AM
Wednesday, April 10, 2019 1:43 PM
All replies
-
write-output "Script being executed by $env:username"
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, April 11, 2019 3:03 AM
Wednesday, April 10, 2019 12:22 PM -
thankyou for you help on that.
I have another question, the script also need show which accounts have not been created because the same username has been used. it needs to give me an error message saying which account hasn't been created
Wednesday, April 10, 2019 1:38 PM -
thankyou for you help on that.
I have another question, the script also need show which accounts have not been created because the same username has been used. it needs to give me an error message saying which account hasn't been created
This is not free script modifying forum. Please contact a consultant to help you with modifying a script you have found.
Please carefully review the following links to set your expectation for posting in technical forums.
This Forum is for Scripting Questions Rather than script requests
Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
\_(ツ)_/
Wednesday, April 10, 2019 1:42 PM -
You'll need to add a Try/Catch block around the code that might generate an error.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6
- Marked as answer by Jon Gomes Thursday, April 11, 2019 8:09 AM
Wednesday, April 10, 2019 1:43 PM -
-
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
LeeJust do it.
Thursday, April 11, 2019 3:04 AM