powershell 2.0 redirection file handle exception - windows

I'm looking for a solution to the The OS handle's position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win32 code or another FileStream. exception that would also work on scripts called within the script containing "the fix".
For the purposes of this question, say that I have two scripts:
foo.ps1
# <fix>
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$objectRef = $host.GetType().GetField( "externalHostRef", $bindingFlags ).GetValue( $host )
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
$consoleHost = $objectRef.GetType().GetProperty( "Value", $bindingFlags ).GetValue( $objectRef, #() )
[void] $consoleHost.GetType().GetProperty( "IsStandardOutputRedirected", $bindingFlags ).GetValue( $consoleHost, #() )
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$field = $consoleHost.GetType().GetField( "standardOutputWriter", $bindingFlags )
$field.SetValue( $consoleHost, [Console]::Out )
$field2 = $consoleHost.GetType().GetField( "standardErrorWriter", $bindingFlags )
$field2.SetValue( $consoleHost, [Console]::Out )
# </fix>
write-host "normal"
write-error "error"
write-host "yay"
.\bar.ps1
bar.ps1
write-host "normal"
write-error "error"
write-host "yay"
And foo.ps1 is being run like this:
powershell .\foo.ps1 > C:\temp\redirecct.log 2>&1
The expected output should be:
normal
C:\foo.ps1 : error
At line:1 char:10
+ .\foo.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,foo.ps1
yay
normal
C:\bar.ps1 : error
At C:\foo.ps1:17 char:6
+ .\bar <<<< 2>&1
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,bar.ps1
yay
However, due to the known bug, the output is actually:
normal
C:\foo.ps1 : error
At line:1 char:10
+ .\foo.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,foo.ps1
yay
normal
out-lineoutput : The OS handle's position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win3
2 code or another FileStream. This may cause data loss.
+ CategoryInfo : NotSpecified: (:) [out-lineoutput], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.OutLineOutputCommand
So the observed behavior is that the changes made by "the fix" aren't being inherited by the 'child' script (bar.ps1, in this case). When bar.ps1 tries to write, it crashes hard. If I don't guard against it somehow in foo.ps1, it will also crash hard. What can I do before/in calling bar.ps1 to prevent bar.ps1 from crashing when it tries to write?
Constraints:
Powershell 2.0
The script must be run as above
I can't modify bar.ps1 (and it should not crash when writing to stderr).
UPDATE
Below is a half-acceptable solution. I say half because it only prevents the 'parent' script from crashing. The 'child' script still fails hard when it tries to write. On the plus side, it can go as far as recognizing that bar failed.
foo.ps1:
function savepowershellfromitself {
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$objectRef = $host.GetType().GetField( "externalHostRef", $bindingFlags ).GetValue( $host )
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
$consoleHost = $objectRef.GetType().GetProperty( "Value", $bindingFlags ).GetValue( $objectRef, #() )
[void] $consoleHost.GetType().GetProperty( "IsStandardOutputRedirected", $bindingFlags ).GetValue( $consoleHost, #() )
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$field = $consoleHost.GetType().GetField( "standardOutputWriter", $bindingFlags )
$field.SetValue( $consoleHost, [Console]::Out )
$field2 = $consoleHost.GetType().GetField( "standardErrorWriter", $bindingFlags )
$field2.SetValue( $consoleHost, [Console]::Out )
}
savepowershellfromitself
write-host "normal"
write-error "error"
write-host "yay"
$output = .\bar.ps1 2>&1
savepowershellfromitself
write-host "$output"
if( $errors = $output | ?{$_.gettype().Name -eq "ErrorRecord"} ){
write-host "there were errors in bar!"
}
write-error "error2"
write-host "done"

If you do this in foo.ps1 it solves the problem:
# <fix>
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$objectRef = $host.GetType().GetField( "externalHostRef", $bindingFlags ).GetValue( $host )
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
$consoleHost = $objectRef.GetType().GetProperty( "Value", $bindingFlags ).GetValue( $objectRef, #() )
[void] $consoleHost.GetType().GetProperty( "IsStandardOutputRedirected", $bindingFlags).GetValue( $consoleHost, #() )
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$field = $consoleHost.GetType().GetField( "standardOutputWriter", $bindingFlags )
$field.SetValue( $consoleHost, [Console]::Out )
$field2 = $consoleHost.GetType().GetField( "standardErrorWriter", $bindingFlags )
$field2.SetValue( $consoleHost, [Console]::Out )
# </fix>
write-host "normal"
write-error "error"
write-host "yay"
powershell .\bar.ps1 2>&1 | more
Piping the output through more hides the fact that it is ultimately going to a file from the child instance of Powershell, bypassing the bug.
In fact, if you create a grandparent script foobar.ps1 which just runs foo.ps1:
powershell .\foo.ps1 2>&1 | more
Then you don't need "the fix" at all, and foo.ps1 can just be
write-host "normal"
write-error "error"
write-host "yay"
.\bar.ps1
because the piping solves the problem for all descendent scripts.

Related

Syntax error with Uninstaller using Powershell

I'm trying to get my universal uninstaller working. Here is my code:
CLS
$Software = "Zoom"
$Filter = "*" + $Software + "*"
$Program = $ProgUninstall = $FileUninstaller = $FileArg = $NULL
try
{
if (Test-Path -Path "HKLM:\SOFTWARE\WOW6432Node")
{
$programs = Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction Stop
}
$programs += Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction Stop
$programs += Get-ItemProperty -Path "Registry::\HKEY_USERS\*\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue
}
catch
{
Write-Error $_
break
}
foreach($Program in $Programs)
{
$ProgDisplayName = $Program.DisplayName
$ProgUninstall = $Program.UninstallString
if($ProgDisplayName -like $Filter)
{
#$Program
$aux = $ProgUninstall -split #('\.exe'),2,[System.StringSplitOptions]::None
$Uninstaller = (cmd /c echo $($aux[0].TrimStart('"').TrimStart("'") + '.exe')).Trim('"')
$UninsParams = $aux[1].TrimStart('"').TrimStart("'").Trim().split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)
if($aux -notlike "param 0 = *")
{
# $UninsParams = $aux[1].TrimStart('"').TrimStart("'").Trim().split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)
}
$Uninstaller
$UninsParams
# . $Uninstaller $UninsParams | Where-Object { $_ -notlike "param 0 = *" }
}
}
In my example I'm trying to get an output for Zoom. I have the Zoom client installed and the Zoom Outlook plugin installed.
Here is the output of the program:
DisplayName : Zoom Outlook Plugin
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{0B76DE11-5937-4491-A66A-617E42170AFF}
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
PSChildName : {0B76DE11-5937-4491-A66A-617E42170AFF}
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
You cannot call a method on a null-valued expression.
At line:34 char:9
+ $UninsParams = $aux[1].TrimStart('"').TrimStart("'").Trim().s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
AuthorizedCDFPrefix :
Comments : Zoom
Contact : Zoom Video Communications, Inc.
DisplayVersion : 5.4.58891
HelpLink : https://support.zoom.us/home
HelpTelephone :
InstallDate : 20201119
InstallLocation :
InstallSource : C:\temp\Zoom\
ModifyPath : MsiExec.exe /X{3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
NoModify : 1
Publisher : Zoom
Readme :
Size :
EstimatedSize : 122109
UninstallString : MsiExec.exe /X{3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
URLInfoAbout : https://zoom.us
URLUpdateInfo :
VersionMajor : 5
VersionMinor : 4
WindowsInstaller : 1
Version : 84207115
Language : 1033
DisplayName : Zoom
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{3109C49B-F5E4-4FEC-8F6F-EC5E4626B36
1}
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
PSChildName : {3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
Part of the problem from what I can tell is the Zoom Outlook plugin doesn't have any uninstall strings associated with it. I'm assuming it's just part of the Zoom Client (even though it's a seperate installer). What I'm trying to do is get this code to work without throwing any errors or displaying false positives.
Here is the output of my 2 parms "$Uninstaller" and "$UninsParams"
You cannot call a method on a null-valued expression.
At line:34 char:9
+ $UninsParams = $aux[1].TrimStart('"').TrimStart("'").Trim().s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
.exe
/X{3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
MsiExec.exe
/X{3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
Thanks in advance!
The problem here is that since the Zoom Client Plugin doesn't have an uninstall script, your -split operation evaluates to a single empty string, and $aux[1] therefore evaluates to $null, hence the error message.
You could filter out entries without a valid UninstallString:
foreach($Program in $Programs |Where-Object UninstallString -match '\.exe')
{
# ... now you don't need to worry about this not resulting in two strings
$aux = $ProgUninstall -split #('\.exe'),2,[System.StringSplitOptions]::None
}

Powershell New-ADUser Variable isnt working

Script
$ADUser = Get-ADUser
if($ADUser -ne "") {echo "there are existing ADUsers!"} #
$Zeilen = (Get-Content C:\Users\Administrator\Documents\users.csv | Measure-Object –Line).Lines #import file, count lines with Measure-Object(mo)(-> output object), ".Lines" takes just number from Measure-Object and convert it to a number
$Zeilen -= 1 # "-1" because 1. line = header without relevant information
$Ausgabe = Import-Csv C:\Users\Administrator\Documents\users.csv
$password = "Ausbildung2020" | ConvertTo-SecureString -AsPlainText -Force #is disabled without password
###################
#creates list with existing users
$Userlist = ""
$CsvData = (Get-ADUser).DistinguishedName | Select-Object -Skip 3 #removes first 3 Zeilen, which contains Admin, Guest und kgrtgt because the are irrelevant
$ZeilenCsvData = $CsvData.count #line amount from (Get-ADUser).DistinguishedName
for($line = 0;$line -lt $ZeilenCsvData; $line++ ) #-lt oder -le ?
{
$UserLine = $CsvData[$line]
$user1 = $UserLine.Split(",")
$user2 = $user1[0].Split("=")
$user3 = $user2[1] #extract Username from String
$UserList += $user3 + "`r`n" #paste Username from current Iteration to $UserList
}
###################
#check if ADUser exists
for ($loop = 0; $loop -lt $Zeilen; $loop++) #-lt oder -le ? #execute loop as often as lines in (Csv-file with (new) Users) exists
{
for($i = 0; $i -lt $ZeilenCsvData ; $i++) #$ZeilenCsvData: number of lines from Get-ADUser without Admin, Guest und kgrtgt
{
if($Ausgabe[$loop].Name -eq $UserList.[$i]) #!!line with problem!!
{
$match = $true
break
}
}
if($match -eq $true){ continue } #ends Iteration and continue with new one if User exists
###################
#create User if it doesn't exist
New-ADUser -Name $Ausgabe[$loop].Name `
-GivenName $Ausgabe[$loop].GivenName `
-Surname $Ausgabe[$loop].Surname `
-City $Ausgabe[$loop].City `
-AccountPassword $password `
-path "OU=Benutzer,DC=dmamgt,DC=local" `
Enable-ADAccount -Identity $Ausgabe[$loop].Name #requirement: password matches standard
}
Problem
In the problem line the „ [$ “ marked red and I get those errors which make the script unexecutable
Task
I got the task to create a organizational unit called "Benutzer" in which I should create 20 Users with some properties like name, city,password, enabled,... which I imported from a csv file. But I have to check whether the user already exists if so the loop should go to the next user to create it.
Errors:
At C:\Users\Administrator\Documents\extended New-ADUser mit csv.ps1:36 char:48
+ if($Ausgabe[$loop].Name -eq $UserList.[$i])
+ ~
Missing type name after '['.
At C:\Users\Administrator\Documents\extended New-ADUser mit csv.ps1:36 char:47
+ if($Ausgabe[$loop].Name -eq $UserList.[$i])
+ ~
Missing property name after reference operator.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingTypename
Method invocation failed because [System.Char] does not contain a method named 'Split'.
At C:\Users\Administrator\Documents\extended New-ADUser mit csv.ps1:23 char:5
+ $user1 = $UserLine.Split(",")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Make $UserList an array:
$UserList = #()
Add items like this:
$UserList += $user
And this is how you index an array (without the dot)
$UserList[$i]
There might be some other issues with your script too, I didn't read it fully, but I hope this will get you started.

Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] does not contain a method named 'op_Division'

I am using the progress bar to check how many computers exist in the “COMPUTERS” container prior to moving of the computers to the specific OU “LAPTOPS. Keep in mind, when only one computer exists in the “COMPUTERS” container, I received the error message below. The computer then gets moved successfully. However, when two or more computers exist in the COMPUTER container, it did NOT project the error when moving from the container to OU. I’ve been searching to find answer but no luck. Please I need some help.
Note: I’m using Powershell version 5.1.14409.1012
error: Does not contain method name 'op_Division'
Method invocation failed because
[Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] does not contain a
method named 'op_Division'.
At line:13 char:5
+ $percent = ($counter / $pcName.count) * 100
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Division:String) [], RuntimeExcepti
on
+ FullyQualifiedErrorId : MethodNotFound
$computerCN = 'CN=COMPUTERS,DC=MYTEST,DC=LOCAL'
$pcName = Get-ADComputer -Filter * -SearchBase $computerCN -SearchScope OneLevel
$computerOU = 'OU=LAPTOP,OU=MYHOME,DC=MYTEST,DC=LOCAL'
$counter = 0
Foreach ( $computer in $pcName )
{
$prefix = $computer.name
$percent = ($counter / $pcName.count) * 100
$counter++
Write-Progress -Activity 'Processing computers' -Status "Scanning... $counter out of $($pcName.count)" -CurrentOperation $prefix -PercentComplete $percent
Start-Sleep -Milliseconds 500
Switch -Wildcard ( $prefix )
{
'LAP*' { 'Moved'; Get-ADComputer $computer | Move-ADObject -TargetPath $computerOU }
Default { 'Skip' }
}
}
Are you certain that GetADComputer is returning more than one object? You should always force results of a command to a collection with the array subexpression operator when you are expecting a collection.
$pcname = #(Get-ADComputer -Filter * -SearchBase $computerCN -SearchScope OneLevel)

How can I add the device names that were not scanned (offline etc) by a PowerShell script

Very very much a PowerShell newbie here I wanted a script to scan devices on the network and report on Local Admins. Found one out there and made some minor modifications to meet my needs - but I have one mod I cant work out how to do. Hoping someone out there will know a simple way to do it ?
The scrip below will read in a list of device names - scan them and output a dated report for all devices that are live and on-line. If the device is not accessible I get the following error on screen but nothing in the report.
I would like when it encounters an error that it writes to the report file - something along the lines of "$computor was not accessible!"
The code I am using is
$date = Get-Date -Format o | foreach {$_ -replace ":", "."}
ECHO "Starting scan"
$Result = #()
foreach($server in (gc .\servers.txt)){
$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)
$Filename = "c:\" + "LocalAdminAudit" + $date + ".txt"
function getAdmins
{
ECHO "SEARCHING FOR DEVICE"
$members = ($Group.psbase.invoke(”Members”) | %
{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}) -
replace ('WinNT://DOMAIN/' + $server + '/'), '' -replace ('WinNT://DOMAIN/',
'DOMAIN\') -replace ('WinNT://', '')
$members}
ECHO "READY TO WRITE OUTPUT"
$Result += Write-Output "SERVER: $server"
$Result += Write-Output ' '
$Result += ( getAdmins )
$Result += Write-Output '____________________________'
$Result += Write-Output ' '
ECHO "Record written"
}
# Added date run to report
$result += Write-Output "Date Reported: $date"
$Result > $Filename
Invoke-Item $Filename
# replace "DOMAIN" with the domain name.
ECHO "Scan Complete"
And the on screen error when a machine is off line or otherwise doesn't respond is
Exception calling "Find" with "1" argument(s): "The network path was not found.
"
At \server\users\User.Name\Powershell Scripts\Get-Local-AdminsV3.ps1:1
0 char:40
+ $Group = $computer.psbase.children.find <<<< (”Administrators”)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
I would like when it encounters an error that it writes to the report file - something along the lines of "$computor was not accessible!" - I am pretty sure there must be an easy way of doing this - but I cant work it out so any tips would be greatly appreciated
As Matt, mentioned in the comments. You can use a Try/Catch block inside your function to catch the error.
I also made some other changes. The most major is that I changed the function to contain all of the code necessary to get the local administrator group. Then the loop just calls the function once per computer with the computer name. This function is then reusable.
Secondly rather than output to a text file, I changed to outputting to a CSV as is a more structured format that can be used better later.
Also rather than relying on writing to the console host, I used Write-Progress to report the progress of the loop.
$Servers = Get-Content .\servers.txt
$ExportFileName = "c:\LocalAdminAudit$date.csv"
function Get-LocalAdministrator {
[cmdletbinding()]
Param(
$ComputerName
)
$Group = [ADSI]("WinNT://$computername/Administrators,group")
try {
$Group.Invoke("Members") | ForEach-Object {
$User = ($_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null) -split '/')[-2,-1] -join '\'
[PSCustomObject]#{
"User" = $User
"Server" = $ComputerName
"Date" = Get-Date -Format o | ForEach-Object {$_ -replace ":", "."}
}
}
}
catch {
[PSCustomObject]#{
"User" = "Failed to Report"
"Server" = $ComputerName
"Date" = Get-Date -Format o | ForEach-Object {$_ -replace ":", "."}
}
}
}
$LocalAdmins = foreach ($Server in $Servers) {
Write-Progress -Activity "Retrieving Local Administrators" -Status "Checking $Server" -PercentComplete (([array]::indexof($Servers,$Server)/($Server.count))*100)
Get-LocalAdministrator $Server
}
$LocalAdmins | Export-CSV $ExportFileName -NoTypeInformation
Invoke-Item $ExportFileName
Lastly, be careful of smart quotes especially when cutting and pasting between Outlook and word.

Put() sometimes throws exception when trying to assign drive letter to partition

I have a simple script that assigns a drive letter to any unlettered partition, like the following:
function GetNextAvailableLetter
{
#returns an unused char for drive letter assignment, or $null if none are available
}
foreach ($disk in ( get-wmiobject -class win32_volume | where-object { $_.DriveLetter -eq $null } ) )
{
$letter = GetNextAvailableLetter
if ( $letter -ne $null )
{
$disk.DriveLetter = $letter + ":"
$disk.Put()
}
}
Oddly, sometimes it'll work, and sometimes Put() throws an exception:
Exception calling "Put" with "0" argument(s): "Not supported"
I have no idea why Put() would throw.
I made a couple of empty, driveletterless drives on my computer and was able to recreate this and one other error that I think you might have neglected to mention.
Property 'DriveLetter' cannot be found on this object; make sure it exists and is settable.
At line:2 char:1
+ $disk.DriveLetter = "Q:"
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Exception calling "Put" with "0" argument(s): "Access is denied.
"
At line:3 char:1
+ $disk.Put()
+ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
The way to solve this would be run the variable $disk though for each loop or something along those lines. Another approach would be to check the $disk.Count ahead of time.
$disk = get-wmiobject -class win32_volume | where-object { $_.DriveLetter -eq $null }
If (($disk) -and ($disk.Count -eq 1)){
$disk.DriveLetter = "Q:"
$disk.Put()
}
The If should in theory protect you from errors when $disk is empty or returns more that one object.
According to the Scripting Guys:
The reason for this error is that the Windows PowerShell prompt is not running with Administrator rights. Unfortunately, the error that bubbles back up from WMI does not tell us that the problem is related to rights.

Resources