Archive

Archive for June, 2015

Powershell behind a Proxy server

June 30th, 2015 No comments

so there I was, living in my Powershell session, when I stumbled across a cmdlet that looked kinda useful.
I wanted a little more info, but when I ran “get-help ” – I discovered that my local help files were out of date.

That’s easy to fix -no?:

Update-Help : Failed to update Help for the module(s) 'Microsoft.PowerShell.Management, Microsoft.PowerShell.Security,
Microsoft.PowerShell.Utility, CimCmdlets, ISE, Microsoft.PowerShell.Diagnostics, Microsoft.PowerShell.Host, Microsoft.WSMan.Management,
PSDesiredStateConfiguration, PSScheduledJob, PSWorkflow, PSWorkflowUtility, Microsoft.PowerShell.Core' with UI culture(s) {en-US} : Unable
to connect to Help content. The server on which Help content is stored might not be available. Verify that the server is available, or
wait until the server is back online, and then try the command again.
At line:1 char:1
+ update-help
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Update-Help], Exception
    + FullyQualifiedErrorId : UnableToConnect,Microsoft.PowerShell.Commands.UpdateHelpCommand

errr . . .no!

Of course, I was in the office – behind a corporate proxy, that needed authentication . . and I needed my Powershell session to use my corporate proxy.

As it goes, the ie proxy settings are managed by GPO, so I figured I’d just use these.

First step – let’s check what our Powershell environment is using for its proxy:

netsh winhttp show proxy


Current WinHTTP proxy settings:

    Direct access (no proxy server).

hmm – not great, let’s change it

netsh winhttp import proxy source=ie
netsh winhttp show proxy

cool – we have now told Powershell to use our ie proxy.
Now, in our environment, we need to authenticate against the proxy.

If you manually authenticate (Username, Password) take this approach:

$webclient=New-Object System.Net.WebClient
$creds=Get-Credential
$webclient.Proxy.Credentials=$creds

Alternatively, if you are in a Domain environment and your proxy accepts NTLM/AD Auth, here’s what to do:

$webClient = new-object System.Net.WebClient
$webClient.Headers.Add(“user-agent”, “PowerShell Script”)
$webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

Magic.

Now all that’s left to do is run

Update-Help

And try remember what it was that you were trying to achieve in the first place.

Categories: Powershell Tags:

Invoking SSH commands using powershell (kinda)

June 30th, 2015 No comments

Sometimes, you need to fire SSH commands from a Powershell session.
This is quite quick and easy if you have the right tools.

First of all, get the Posh-SSH cmdlets off github:
https://github.com/darkoperator/Posh-SSH

Install this like any other module (Drop it in your PSModulePath – else amend the code below to import the module from wherever you would like to execute it)
Now simply fire away using the module.

clever work (none of the credit to me)

I’ve used this for invoking SSH commands against ESX hosts, Dell iDracs and other SSH devices. Simple and quite reliable. I find it far more consistent than plink.exe etc

# Quick and untested copy of what you need:

Import-Module Posh-SSH

# Create Pwd (this saves a file copy)
# read-host -assecurestring | convertfrom-securestring | out-file $env:userprofile\$env:username.txt

# Retrieve info from file previously created under user profile
$username = "username@domain.net.intra"
$password = get-content $env:userprofile\$env:username.txt | convertto-securestring
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password

# Create SSH Session
$ssh = New-SSHSession -ComputerName $vmhost -Credential $credential -AcceptKey 1

# Invoke SSH command and capture output as string (you can return the full object if you like, I just needed the string Out)
$ret = $(Invoke-SSHCommand -SSHSession $ssh -Command "racadm getconfig -g cfgUserAdmin -i 2").Output

# return object is a String - if you want it as an array of strings for each row returned
$ret = $ret.split("`n")


# Clean up your mess
Remove-SSHSession -SSHSession $ssh
Remove-variable ssh

Categories: Powershell Tags: