Invoke-Command: The Best Way to Run Remote Code

IT professionals rarely work just on our local computer. Using the PowerShell Invoke-Command cmdlet, we don’t have to! This cmdlet allows us to seamlessly write code as if we were working on our local computer. By using the PowerShell Remoting feature, The Invoke-Command cmdlet is a commonly used PowerShell cmdlet that allows the user to execute code inside of a PSSession. This PSSession can either be one created previously with the New-PSSession cmdlet or it can quickly create and tear down a temporary session as well. Think of Invoke-Command as the PowerShell psexec. Though they are implemented differently, the concept is the same. Take a bit code or command and run it “locally” on the remote computer. For Invoke-Command to work though, you must have PowerShell Remoting enabled and available on the remote computer. By default, all Windows Server 2012 R2 or later machines do have it enabled along with […]

Read more

How to Find Monitor Size for a Local or Remote PC

Here’s a pretty cool script that will get you the monitor size for a local or remote device via WMI. If will work with both single monitor devices as well as multiple monitors. < pre class=”” line=”1″ lang=”powershell”>param($ComputerName = ‘COMPUTERNAME’) $output = [PSCustomObject]@{ComputerName = $ComputerName;MonitorSizes=”} $oWmi = Get-WmiObject -Namespace ‘root\wmi’ -ComputerName $ComputerName -Query “SELECT MaxHorizontalImageSize,MaxVerticalImageSize FROM WmiMonitorBasicDisplayParams”; $sizes = @(); if ($oWmi.Count -gt 1) { foreach ($i in $oWmi) { $x = [System.Math]::Pow($i.MaxHorizontalImageSize/2.54,2) $y = [System.Math]::Pow($i.MaxVerticalImageSize/2.54,2) $sizes += [System.Math]::Round([System.Math]::Sqrt($x + $y),0) }##endforeach } else { $x = [System.Math]::Pow($oWmi.MaxHorizontalImageSize/2.54,2) $y = [System.Math]::Pow($oWmi.MaxVerticalImageSize/2.54,2) $sizes += [System.Math]::Round([System.Math]::Sqrt($x + $y),0) }##endif $output.MonitorSizes = $sizes $output

Read more

Why File Copies Through WinRM is Awesome

When it was announced that Copy-Item would get -ToSession  and -FromSession  parameters I originally just blew it off. I thought to myself why would I ever need that? Why not just use SMB? It works. Why do we need a way to pass files to and from PS sessions? It wasn’t until I came across a real-world scenario that finally made the light bulb come on. I’ve been working on a project that requires deploying Hyper-V VMs into a restricted DMZ environment. As part of this project I’m required to install some software onto these VMs. We had the code to do this. However, the code was using SMB to pull bits from a UNC share on the internal network. Normally, this works fine but this was the first time we were trying to deploy in the DMZ. Being a DMZ, it’s network access was limited thus only WinRM ports […]

Read more

Forget Remote Desktop Already!

People, Let’s put the Remote Desktop client down, OK? I know it’s been a blankie we’ve all had for a very long time but it’s time to finally sell off the blankie at the next garage sale. It’s 2014. We have some cool tools now like Powershell remoting and Server Manager to use. Hell, we’ve had many tools in an IT guy’s toolbox for a long time. Remote WMI through DCOM,  psexec  and admin file shares are a few I can think of off the top of my head. Why do I still see people RDPing to every server to under the sun to do everything? Shift your mindset here. Quit thinking of servers as desktops. Servers don’t have monitors for a reason. Servers are resources; pools of computational power if you will. They are not there to RDP into to surf the web, check your email or do 95% of […]

Read more

Psexec is Dead

UPDATE 11/29/14: OK, fine. I’ll give you the argument that the  -i  switch for  psexec  is unparalleled in the Powershell world and maybe the  -s  switch 🙂 I’ve used Sysinternals’ psexec utility for a really long time. I can’t recall how long it’s been. It used to be one of those goto tools in my software utility tool belt. That is, until Powershell came along. Powershell is an enormously helpful product. Replacing  psexec  is just a single, tiny piece of what it has done for me. So, why am I not using  psexec  anymore? Here’s why: Using psexec requires an external dependency. It’s an external file that must exist somewhere. I can’t count how many times I went to use it and I forget where I put it so I either have to search or download it again. I’ve written scripts years ago and depend on it that I’ve long forgotten about. I run them […]

Read more

Enable PS Remoting Remotely

Download this Enable-PSRemotingRemotely script via the Technet Script Repository I had an instance where I needed to quickly enable PS Remoting on a few remote computers so this is the result.

Read more

Powershell Script Remote Execution

10/25/14 UPDATE: Replaced custom Test-Ping function with Test-Connection Download this script on the MS Script Repository Today I needed to quickly deploy a piece of software to a few dozen computers. Rather than hassle with creating a ConfigMgr application and deploying it I decided to just script it. I needed a way to take the contents of a folder with a Powershell script inside, copy the folder to the remote computer and then execute the script afterwards. This is what I came up with and after a few iterations, it ended up working great.

Read more