Forum Discussion
Get the output of a command into a string
Hi,
I'm new to Powershell and trying to automate some simple tasks.
So basically I need to create a script to check for newly added users on my AD for the last x days (this is already done), and then check each of those new users if they are added on priviledged groups, like Domain Admins, Enterprise Admins or Schema Admins, and raise a warning if any of those new users are on those groups.
I have the first part to check for new added users created and outputting to terminal, but now I need input those SamAccountName's into a variable so then I can use that variable to check for each user groups. How can I do that?
Or is there any other easier approach?
Thanks
Hello, write the output of first step to external file, examplae text.csv
with these file you can read each line and add the variable with content of the file.Get-Content .\test.csv | ForEach-Object { if($_ -match $temp){ #I add write-output to show the content of the file test.csv[n] Write-Output $_ } }
9 Replies
- yuzoyoxIron Contributor
Hello, write the output of first step to external file, examplae text.csv
with these file you can read each line and add the variable with content of the file.Get-Content .\test.csv | ForEach-Object { if($_ -match $temp){ #I add write-output to show the content of the file test.csv[n] Write-Output $_ } }
- dmarquesgnIron Contributor
Hi, thanks, that is great!
So now I got the output in a text file, with this structure:
SamAccountName
--------------
User1
User2
User3So I can make the next step I need to clean up the first 2 lines of the output text file, so I can have the needed values to compare only.
I've searched and tried to use this command:
Get-Content $Path | Select-Object -Skip 3 | Set-Content $Path
If I run this on the file without the Set-Content $Path it works just fine, but when goes to write to the output file, I always got the error:
Set-Content : The process cannot access the file 'C:\Temp\teste.txt' because it is being used by another process.
I've read about it, and some say it's related to having Powershell ISE opened, but I close it and it's just the same.
Any tips on this?Thanks
- yuzoyoxIron Contributorit is not the best option, but you can export to a new name file, example test2.txt, or temp.txt, and in the end of execution execute the delete command for the files that you dont want to keep