PowerShell script to get logged in user - powershell

I am looking to run a script on a remote machine using an automation tool that runs the scripts in the system context.
What I have so far:
$userId = Get-Process -IncludeUserName explorer | % username | sort Username -Unique
Write-Host $userid.ToLower()
Results:
Get-Process : A parameter cannot be found that matches parameter name
'IncludeUserName'.
At line:1 char:39
+ $userId = Get-Process -IncludeUserName <<<< explorer | % username | sort Username -Unique
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
ToLower : You cannot call a method on a null-valued expression.
At line:2 char:27
+ Write-Host $userid.ToLower <<<< ()
+ CategoryInfo : InvalidOperation: (ToLower:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any ideas how to help this script? Or the cause of the errors?

The error says it all; the switch -IncludeUserName is not available on your computer. That's because it requires PowerShell 4.0 or above like Ansgar mentioned.
One solution is to install the latest Windows Mangement Framework (WMF) which includes the latest version of PowerShell.
You can also use the WMI-class Win32_Process to get the user and/or domain by calling the object's GetOwner()-method. Ex:
Get-WmiObject -Class Win32_Process -Filter "Name = 'explorer.exe'" |
ForEach-Object { $_.GetOwner() | % { "$($_.Domain)\$($_.User)" } } |
Sort-Object -Unique

the following script might help you. I'm not completely certain if the invoked command will get the remote logged in user, because I'm not on a work computer right now but it came from the internet so it must be true.
$Computers = (Get-Content "\\<sharedrive\<directory>\Computers.txt)
Foreach ($Computer in $Computers){ `
Invoke-Command -ComputerName $Computer -ScriptBlock `
{Get-WMIObject -Class Win32_ComputerSystem).Username} `
}

Related

Update definition of SCEP on windows servers using powershell

i am trying to update the definition of SCEP on remote Windows Servers using MpCmdRun.exe which exists under "C:\ProgramData\Microsoft\Windows Defender\platform\*\MpCmdRun.exe. Unfortunately it is not accepting -filepath. says its null or empty. below is my code
$comp = "SRV1234"
$MpCmdRun = invoke-Command -ComputerName $comp -ScriptBlock {get-item -Path "C:\ProgramData\Microsoft\Windows Defender\platform\\*\MpCmdRun.exe" | Sort-Object -Property LastWriteTime -Descending |Select-Object -ExpandProperty fullname -First 1}
invoke-Command -ComputerName $comp -ScriptBlock {Start-Process -FilePath $MpCmdRun -ArgumentList "-signatureUpdate" -Wait}
Below is the error:
Cannot validate argument on parameter 'FilePath'. The argument is null
or empty. Provide an argument that is not null or empty, and then try
the command again.
+ CategoryInfo : InvalidData: (:) [Start-Process], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCommand

Powershell printer installation script

I'm trying to create a login script that performs a few actions. I need it to complete the following.
Check and record the current default printer.
Delete all printers.
Install a specific list of printers.
Set the default printer back to what the default printer was previously.
I need to do this because we've made some printer name changes on the network and we want to re-map all of the printers with the new names and also install some additional printers. I currently have a batch file calling a .ps1 file. Here's the code from both files.
Login.bat
#echo off
powershell -command Set-ExecutionPolicy Unrestricted -force
Powershell -command " & '\\server\share\MyPSScript.ps1'"
Login.ps1
# Save current default printer.
Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true" | foreach {$_.portname} -OutVariable CurDefPrinter
# Deletes all network printers.
Get-WmiObject Win32_Printer | where{$_.Network -eq ‘true‘} | foreach{$_.delete()}
# Maps all network printers.
add-printer -connectionname "\\server\ricoh"
add-printer -connectionname "\\server\hp"
# Set default printer
(Get-WmiObject -Class Win32_Printer -Filter "portame='$CurDefPrinter'").SetDefaultPrinter()
When I run this, I get this error.
C:\Temp\Scripts>Login.bat
C:\Temp\Scripts>REM #echo off
10.0.0.155
Get-WmiObject : Invalid query "select * from Win32_Printer where portame='10.0.0.155'"
At \\server\share\login.ps1:12 char:2
+ (Get-WmiObject -Class Win32_Printer -Filter "portame='$CurDefPrinter' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
You cannot call a method on a null-valued expression.
At \\server\share\Login.ps1:12 char:1
+ (Get-WmiObject -Class Win32_Printer -Filter "portame='$CurDefPrinter' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any help would be greatly appreciated!
You spelled PortName wrong so nothing was being returned and you were calling a method on ($null)
(Get-WmiObject -Class Win32_Printer -Filter "portname='$CurDefPrinter'").SetDefaultPrinter()
If you dont need to use the Query and Filter option you can do it like this:
#to get the current default printer
$default = Get-WmiObject -Class Win32_Printer | Where-Object {$_.Default -eq "true"}
#to reset the default printer
$default.SetDefaultPrinter()
Atleast it works at my enviroment.

Unable to delete certain files and folders using Powershell

I need to create a script that clears the Lync cache on remote machines. To do so I need to close Outlook and Lync if they are open, and then clear out two folders whilst deleting another. Here is what I have so far:
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$computer = [Microsoft.VisualBasic.Interaction]::InputBox("Which computer do you wish to clear the Lync cache for?", "Computer", "$env:computername")
$username = Get-WMIObject -class Win32_ComputerSystem -ComputerName $computer | select username
$RSA = "C$\Users\$username\AppData\Roaming\Microsoft\Crypto\RSA"
$Tracing = "C$\Users\$username\AppData\Local\Microsoft\Office\15.0\Lync\Tracing"
$SIP = "C$\Users\$username\AppData\Local\Microsoft\Office\15.0\Lync"
(Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match "Outlook" }).Terminate()
(Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match "Lync" }).Terminate()
(Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match "Communicator" }).Terminate()
start-sleep -s 3
Get-ChildItem -path \\$computer\$RSA -include * | Remove-Item -recurse -force
Get-ChildItem -path \\$computer\$Tracing -include * | Remove-Item -recurse
Remove-Item -path \\$computer\$SIP\*sip* -recurse -force
The script closes the programs as required, but it doesn't appear to do anything with the folders. The only errors I get are below:
You cannot call a method on a null-valued expression.
At C:\Users\*myusername*\Desktop\Personal Powershell scripts\Clear Lync cache.ps1:10 char:1
+ (Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\*myusername*\Desktop\Personal Powershell scripts\Clear Lync cache.ps1:11 char:1
+ (Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\*myusername*\Desktop\Personal Powershell scripts\Clear Lync cache.ps1:12 char:1
+ (Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
ISE is open as admin. I have admin rights to the domain as well as the folders in question. Can anyone see where I'm going wrong?

Invalid Namespace when automating Get-WMIObject

I am trying to automate the retrieval of OS architecture and when I do I get an Invalid Namespace error.
foreach($i in $hosts){
$ip = $i.name
Get-WmiObject -ComputerName $ip Win32_OperatingSystem
}
If I take Get-WmiObject -ComputerName $ip Win32_OperatingSystem and replace $ip with a legit IP it works perfectly. I even added Write-Host to the front of Get-WMIObject so it would display the entire command that would run. I ran the output from Write-Host and the command completes successfully.
Error:
Get-WmiObject : Invalid namespace
At line:4 char:18
+ Get-WmiObject <<<< -ComputerName $ip Win32_OperatingSystem
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
The $hosts variable is a PSCustomObject with two noteProperty fields. The first is name and the second is user. The name field holds an IP address of the system.
It turns out there were trailing whitespaces after the IPs. I used .trim() and now everything works just fine.

Error handling - PowerShell script

I have the following powershell script that cycles through a list of hostnames and changes the DNS settings for the active interfaces:
$servers = Get-Content C:\users\kevin.todd\desktop\serverlist.txt
foreach($server in $servers)
{
Write-Host "Connect to $server..."
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}
$newDNS = "10.100.10.81","10.100.10.82"
foreach($nic in $nics)
{
Write-Host "`tExisting DNS Servers " $nic.DNSServerSearchOrder
$x = $nic.SetDNSServerSearchOrder($newDNS)
if($x.ReturnValue -eq 0)
{
Write-Host "`tSuccessfully Changed DNS Servers on " $server
}
else
{
Write-Host "`tFailed to Change DNS Servers on " $server
}
}
}
The problem is on some hosts I get the following error:
Get-WmiObject : The RPC server is unavailable. (Exception from
HRESULT: 0x800706BA) At C:\Documents and
Settings\user1\desktop\changednsserver.ps1:20 char:26
+ $nics = Get-WmiObject <<<< Win32_NetworkAdapterConfiguration
-ComputerName $server -ErrorAction Inquire | Where{ $_.IPEnabled -eq
"TRUE"}
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject],
COMException
+ FullyQualifiedErrorId :
GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Existing DNS Servers You cannot call a method on a null-valued
expression. At C:\Documents and
Settings\user1\desktop\changednsserver.ps1:30 char:42
+ $x = $nic.SetDNSServerSearchOrder <<<< ($newDNS)
+ CategoryInfo : InvalidOperation:
(SetDNSServerSearchOrder:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
And I'm asked the following question by Powershell:
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help
(default is "Y"):
I would like the script to just answer A - Yes to all and continue running the script. The problem is it just halts the script until I manually enter "A". How can I have it automatically answer and continue?
Well the short answer is to not tell it to stop in the first place:
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}
Try:
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction SilentlyContinue | Where{$_.IPEnabled -eq "TRUE"}
If you need to see where Errors occur, you have also a possibility to define an error variable using the switch
-ErrorVariable $<variable>
If errors occur in a foreach loop, the Error variable will be an array of error messages you can later analyze by querying the defined error variable with an array index echo $<EVariable>[<index>]
it would be something like echo $MyErrorArray[0] to get the first occurred error in that array.
An elegant way would be to have all that added to a textbox/combobox for log purposes but the coice of error handling ananalyzing is yours.