I'm running a .PS1 script as part of a larger script to get computers to join our domain semi-automatically. The .PS1 is created from variables and ends up looking like this:
add-computer -DomainName ourdomain.com - OUPath "OU=Computers,OU=Somewhere,DC=OURDOMAIN,DC=COM" -Cred OD\syswdg
While this works fine, I would like to be able to output the restult of this to a text file so that I can check if this has worked sccessfully or not before proceeding to do other stuff in the script. Is there any way to get the results of this output to a file? I've tried using the Out-File Cmdlet, the Tee-Object Cmdlet and tried running the joindomain.PS1 from another PS1 like joindomain.ps1 > outputfile.txt and while they all produce a file, it is always empty. Any help appreciated.
By default, there is no output when the cmdlet is successful. Use -Passthru and -Verbose if you want to see the output of this cmdlet.
Related
I want to execute a powershell script file, it works but if I am in the same path that the script
....
$toto=(Get-ChildItem -Path "C:\Users\toto\Desktop\titi\tutu)
I work with the variable $toto
I execute the script from :
PS C:\Users\toto\Desktop\titi> C:\Users\toto\Desktop\titi\script.ps1 = it works
but when I am below s subdir, not knowing the current "working directory" :
C:\Users\toto\Desktop\ C:\Users\toto\Desktop\titi\script.ps1
how to resolve that please
I'm guessing you need something like this:
$toto=(Get-ChildItem -Path "$PSScriptRoot\tutu")
I have the following 2 commands.
Command 1:
Get-QCSyncHistoryStep -Workflow "CSV2AD" -Name "Provision from CSV to 31AD" -Count 1 | ForEach {$_.SuccessOperations} | Out-File c:\scripts\temp1.txt
Command 2:
Get-QCSyncHistorySummaryRun * | Out-File c:\scripts\temp2.txt
Command 1 works only on powershell prompt but not as a script. Where as command 2 works on both prompt and when used in script file.
If I run both the command in a script file, the command 1 is not writing anything into the file where as command 2 dose.
Need help in deciphering the issue.
Thanks.
Your problem may be that Out-File overwrites the contents of the destination file unless you specify the -Append switch, which can still be used if the file doesn't already exist. I'm not familiar with the Get-QCSyncHistory* cmdlets or their output (are they your own functions?), but I suppose that if you pipe the objects in the output through a foreach loop and the final object has a null-valued SuccessOperations parameter, then you'll effectively just wipe the file you have been trying to write to.
I have a ps script like:
Start-Process powershell -Verb runAs -ArgumentList "Get-WebApplication"
Because Get-WebApplication needs admin right, so I use "-verb runAs".
My question is: now the result only shows in the new powershell windows. How can I make my main powershell get/display the result?
You have two choices that I see:
Run your script as administrator and just run Get-WebApplication directly.
Capture the output from the powershell process you're starting with Start-process and then do whatever you want with the output in your script. To capture the output you can either redirect the output to a file and then read the file in your script, or you can use .NET [diagnostics.process]::start to redirect stdout and directly read the output stream in your script (no file is created).
I am trying to create a PowerShell script by another PowerShell script. I have something like:
>
$scriptBlock = {
write-host "this is the body of the script that I want to add to another PS script"
if($true){
write-host "so that I can execute this automatically created script somewhere "
}
}
$scriptBlock | Out-String -Width 4096 | Out-File "c:\test.ps1"
AtRunUnattended.exe "$($Dict.Get_Item("MASTER_DIRECTORY_PATH"))\MEDIA_FILE\" /S /noreboot /L logfile="%TEMP%\AT $($Dict.Get_Item("PRODUCT_NAME")) V8.4.log" altsource="C:\temp\baf\mediafile"
However, when I have some long single-line scripts like the one shown above, it gets automatically wrapped so the out-put .ps1 file would not work as if I had invoked the script directly from the parent script.
So in the .ps1 file that the parent .ps1 script created, the code then looks like this:
ElseIf($str.StartsWith("DRIVER_DATE=")){$str = "DRIVER_DATE=$(Get-Date
-f MM-dd-yyyy))"}
which will not run properly if it is run.
So does anybody know how to text-format scriptblocks so that they can be properly written to another child script file for further execution?
I did some research and I think that it might have something to do with PS's internal text buffer width or something. I tried other out-file methods as well like [System.IO.StreamWriter], however all of them look the same--wrapped and limited to a certain width per line.
Please help me, thank you!
The entire purpose of this thing is to generate some scripts automatically and remotely execute these created scripts on other machines.
Use the -Width parameter with the Out-File cmdlet as follows:
Out-File -FilePath "c:\test.ps1" -Width 4096
the variable will not be expanded in single quote. May be you need a sample like this:
$scripts=#'
$date=get-date
"Hello,now is $date"
'#
$scripts | Out-File second.ps1
./second.ps1
Output is:
Hello,now is 09/23/2013 23:41:18
Is there any other way except $MyInvocation.InvocationName in powershell to get the script name?
As i need to turn my script in an exe and in that case it doesnt work on that exe.
I'm assuming since you convert the powershell script to an executable that you are after the location of the executable. You can get it this way:
[Environment]::GetCommandLineArgs()[0]
If you want something that works within and outside of ISE you can use
$MyInvocation.InvocationName
Since full paths and .\YourScript.ps1 can be returned you can parse the name with:
[Regex]::Match( $MyInvocation.InvocationName, '[^\\]+\Z', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::SingleLine ).Value