Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Friday, January 21, 2011

POWERSHELL: Accessing the internet thru a Proxy

Today I needed to make this script work behind a proxy, something that I have found in the past but was not able to find a good set of instructions.
I got it working and I wanted to share it with you, in case someone else is looking for it.

all you need to do is add the following 4 lines anytime before downloading the data (I prefer to do it right after instancing the WebClient object):


$proxyObject = new-object System.Net.WebProxy("MyProxyServer", 8080)
$proxyObject.BypassProxyOnLocal = $true
$proxyObject.Credentials = [System.Net.CredentialCache]::DefaultCredentials
[System.Net.GlobalProxySelection]::Select = $proxyObject


I hope it helps.
Best regards, Marianok.

Wednesday, July 30, 2008

Powershell: Confirmation Level

Part of the scripts I'm making for work involve the update of several Exchange Distribution lists in an automated way.
I was on this when I run into an unexpected behavior: the Remove-DistribtionListMember Command was asking for confirmation, even when I had not specified the -Confirm parameter .

A glance to the exchange.ps1 reveled that, in effect, the Powershell environment for exchange has the confirmation Preference set to HIGH by default (thus asking for confirmation on any task that performs a important change to AD)

Changing the confirmation level it's as easy as :

$ConfirmPreference = 'High' # The allowed values seem to be High, Medium, Low, None.


But, I didn't like the idea of changing the value for the whole environment, who knows what would I like to run next ...

So decided to implement a simple temporary variable to store the value of $ConfirmPreference on the script's start and revert it to normal just before the end.

$ConfirmPreference =$OLD_ConfirmPreference

... # My Code here


$OLD_ConfirmPreference =$ConfirmPreference

My next thinking was on how cool would it be to be able to implement a confirmation switch on my own scripts, and I realize that it's easy to do:

Param(
[switch] $Confirm
)

$OLD_ConfirmPreference =$ConfirmPreference

if ($Confirm) {
$ConfirmPreference = 'High'
} else {
$ConfirmPreference = 'None'
}

...... # My Code here

$ConfirmPreference =$OLD_ConfirmPreference



Now I can have (and give to my script users) control on the Powershell confirmations.


Tuesday, July 29, 2008

The PATH in Powershell

One of the things I wanted to do when I had a Blog was to post a recap of the interesting things I learn periodically, specially if it took me a while to learn them or if there was some complications for me to understand them.
I will start this today with this posting.


The PATH in Powershell

Maybe because I never formally read much about powershell, maybe because I did not bother enough to find it sooner, or for whatever reason, I had never found out what to do in order to be able to execute my scripts as just another PS command, meaning be able to use
my-script
instead of
. my-script.ps1

Yesterday I decided to research on it and after some goggling I got a clue: The Script's directory MUST be in the PATH, meaning that even if you are standing right in the Script's directory, you won't be able to execute it as a command unless the directory is in the PATH. Crazy, isn't it ???

Okay,now that I knew that I need to figure out 2 things:
  1. How to read the path from Powershell (I found that Path and $path did not work), and
  2. How to change it (To be able to add my own directories)

A little more googling revealed that to get the PATH I only need to query the Environmental variables:
$env:path

Easy, right ???? ... Then, Why didn't I figure it our sooner ???

To add items to the pat, it's just as simple:

$env:path = $env:path + ";E:\My Scripts\Powershell\"

Now, that script that i was only able to execute as:
& "E:\My Scripts\Powershell\my-script.ps1"
can now be executed as:
my-script


To see if an specific directory is part of the PATH, we can use simple string functions:

($env:path -match 'My Scripts\\Powershell')

(Notice the double slash in order for the slash not to be considered as a REGEX command)

Well, not "rocket science", but it did took me a while to put all the pieces together, and I hope that I'll save some time to the next person trying to accomplish this.