Showing posts with label PowerShell Script. Show all posts
Showing posts with label PowerShell Script. Show all posts

Wednesday, 27 July 2011

Uploading all files from Directory to FTP from PowerShell Script

Issue:
     To Upload multiple files from Directory which matches the criteria like '.txt' '.exe' MIME Type.

Solution:
     To read all file from the Directory and uploading one by one. The code will be,
             #we specify the directory where all files that we want to upload are contained
             $Dir=<Path of the Directory>
             #ftp server
             $ftp = <FTP Path>
             $user = <UName>
             $pass = <Passwd>
             $webclient = New-Object System.Net.WebClient
             $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
             #list every sql server trace file
             foreach($item in (dir $Dir "*.txt"))
            {
                   "Uploading $item..."
                   $uri = New-Object System.Uri($ftp+$item.Name)
                   $webclient.UploadFile($uri, $item.FullName) 
            }


Uploading a file to FTP from PowerShell Script

Issue: 
    To Upload a file to FTP with proper credentials.

Solution:
    Giving the Address and proper credentials, and code will be,
         
      $File=<File to Upload>
      #ftp server
      $ftp = <FTP Path + File Name>
      $user = <UName>
      $pass = <Passwd>
      $webclient = New-Object System.Net.WebClient
      $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
      "Uploading $item..."
      $uri = New-Object System.Uri($ftp)
      $webclient.UploadFile($uri, $File)

[Note:]
Files will get uploaded properly if and only if the FTP allows You with Access Permissions and Credentials (if having).

Execution of script disabled by the system in PowerShell


Issue:
     Execution of the Shell Script is blocked by the Rules of PowerShell.

Check:
     Start Powershell prompt as the Administrator, and type the following,
             Get-ExecutionPolicy
     If it shows as 'Restricted'.
     You have to reset the policy to allow You to execute by typing,
Syntax

Set-ExecutionPolicy [-ExecutionPolicy] {<Unrestricted> | 
<RemoteSigned> | <AllSigned> | <Restricted> | <Default> |
<Bypass> | <Undefined>} [[-Scope] {<Process> | <CurrentUser>
| <LocalMachine> | <UserPolicy> | <MachinePolicy>}] [-Force]
 [-Confirm] [-WhatIf] [<CommonParameters>]   
 
 Description
          The Set-ExecutionPolicy cmdlet changes the user preference for the Windows PowerShell execution policy. 


Parameters


-ExecutionPolicy <ExecutionPolicy>

Specifies the new execution policy. Valid values are:
-- Restricted: Does not load configuration files or run scripts. "Restricted" is the default execution policy.
-- AllSigned: Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer.
-- RemoteSigned: Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
-- Unrestricted: Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.
-- Bypass: Nothing is blocked and there are no warnings or prompts.
-- Undefined: Removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy that is set in a Group Policy scope.




 

Sending Mail thru gmail server in PowerShell

To Send the mail,
 
 $SmtpClient = new-object system.net.mail.smtpClient
 $smtpclient.Host = <Host> like smtp.gmail.com, smtp.live.com
 $smtpclient.Port = <Port No>
 $smtpclient.EnableSsl =<Boolean Value> like, $true or $false
 $smtpClient.Credentials = [Net.NetworkCredential](Get-Credential GmailUserID)
 $smtpClient.Send(<From Mail>,<ToMail>,<Subject>,<Body>)

You can run it with PowerShell Script or Direct Console itself.

Monday, 25 July 2011

Using Configuration File with Windows PowerShell


Issue: 
    To use the config file for adding the lookup values for PS Scripting.

Solution:

SETTINGS.TXT
#from http://tlingenf.spaces.live.com/blog/cns!B1B09F516B5BAEBF!213.entry
#
[General]
MySetting1=value

[Locations]
InputFile="C:\Users.txt"
OutputFile="C:\output.log"

[Other]
WaitForTime=20
VerboseLogging=True

POWERSHELL COMMAND
#from http://tlingenf.spaces.live.com/blog/cns!B1B09F516B5BAEBF!213.entry
#
Get-Content "C:\settings.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }



Then,
After executing the code snippet, a variable ($h) will contain the values in a HashTable.
Name                           Value
------                              -----
MySetting1                     value
VerboseLogging                 True
WaitForTime                    20
OutputFile                     "C:\output.log"
InputFile                      "C:\Users.txt"


*To get an item from the table use the command $h.Get_Item("MySetting1").*