Feeds:
Posts
Comments

Archive for the ‘Powershell’ Category

I needed to download directory from ftp server modify it and uploaded back to server using powershell. At fist i found that [System.Net.WebRequestMethods+Ftp]::UploadFile and [System.Net.WebRequestMethods+Ftp]::DownloadFile works fine and in real life when my zip file size is bigger it does not work.

I have not found any solution to make it working so I used alternative way to solve it. I have written two batch fildes upload.bat and downlaod.bat and calling these batch files from my PS Script to execute.

Actually if anyone use bat file he/she need not to call the bat file from PS Scripts but in my case after downloading zip file from ftp server I modified many things in the files using powershell and then upload it back to server.

Here are the batch files you can try if needed upload.bat and download.bat

Advertisements

Read Full Post »

to rename a file we use Rename-Item command in powershell the command is very simple

Rename-Item $source $dest -force

But what about changing a folder/directory name? I made mistake to write the same command in case of folder/directory rename and it does not work, shows exception file not found which proves that the command is searching for a file not ready to rename and folder/directory.

After googling for some minutes I found the solution and the command is

Rename-Item -path $destFolder -newName $folderNewName

Example: Rename-Item -path ‘C:\AshrafTest\MyFolder’ -newName ‘MyFolder_New’

By the way the folder should contain some data to be rename. Pretty simple but wasted time to search internet so sharing 🙂

Read Full Post »

As I am working with latest version of Powershell, when I tried to install latest version in XP I found that older version of powershell is available in software update for XP it is easy to find out and remove. But I really faced a big problem when I tried to install that in windows server 2003. I found all the software updates are named “Update of windows Server…” or “Security Update for windows server …” or “Hotfix for windows server 2003 …”. Its really hard to find out the right one and remove. After searching I found that I need to search for KB* to remove the older version.

Older version of powershell should be resembled any of the following

Hotfix for Windows Server 2003 (KB926139)
Hotfix for Windows Server 2003 (KB926140)
Hotfix for Windows Server 2003 (KB926141)

So find out the right one and remove it. Sometime it shows like that KB926139-v2, so don’t be confused. And after click on remove it will show you in the wizard that you are removing poweshell version 1.0 or anything else to be confirm you are really removing the right thing.

Read Full Post »

if you just need to install any software automatically using powershell you can just run the silent moder of the installer from powershell. The poweshell script to run exe or msi in silent mode is:

$command = “cmd.exe /c D:\MyInstaller.exe /s /v`”/qn”
$process = [WMICLASS]”\\PCName\ROOT\CIMV2:win32_process
$process.Create($command)

Remember your installer should have silent mode on in it.

Read Full Post »

I have a text file and in that file list of files and folder are available to copy from a local computer to a remote computer. This is very easy to do using powershell. Example is as follows

$remotePCName = “remotePC”
$remoteRoot = “\\$remotePCName\c$”
$DestFolder=”$remoteRoot\FileCopy\dest”
$SrcFolder=”C:\FileCopy\src”
$InputFile =”C:\FilesToExport.txt”

foreach ($FileLine in Get-Content $InputFile)
{
$src = $FileLine
$dest = $src.Replace($SrcFolder, $DestFolder)
if(!(test-path -path $dest))
{
if((get-item $src).PSIsContainer)
{
new-Item $dest -type Directory -force | out-Null
Copy-Item “$src\*” $dest -recurse
“Folder $dest Missing. Creating it!”
}
else
{
new-Item $dest -type File -force | out-Null
Copy-Item $src $dest -force
“File $dest Missing. Creating it!”
}
}
else
{
if((get-item $src).PSIsContainer)
{
Copy-Item “$src\*” $dest -recurse
“Copy file done for $src”
}
else
{
Copy-Item $src $dest -force
“Copy file done for $src”
}
}
}

a file syncronizer can be created by some code modification easily.

Read Full Post »

I have just wrote a simple method to start/stop remote services from powershell

function ManageRemoteService
{
param([string]$serverName, [string]$serviceName, [string]$status)
if (-not ([appdomain]::CurrentDomain.getassemblies() |? {$_.ManifestModule -like “system.serviceprocess”}))
{
[void][System.Reflection.Assembly]::LoadWithPartialName(‘system.serviceprocess’)
}
$getServiceStatus = (new-Object System.ServiceProcess.ServiceController($serviceName,$serverName)).Status
if($getServiceStatus -ne $null)
{
if($getServiceStatus -eq “Running” -AND $status -eq “Stop”)
{
(new-Object System.ServiceProcess.ServiceController($serviceName,$serverName)).Stop()
(new-Object System.ServiceProcess.ServiceController($serviceName,$serverName)).WaitForStatus(‘Stopped’,(new-timespan -seconds 10))
“$serviceName stopped”
}
elseif($getServiceStatus -eq “Stopped” -AND $status -eq “Start”)
{
(new-Object System.ServiceProcess.ServiceController($serviceName,$serverName)).Start()
(new-Object System.ServiceProcess.ServiceController($serviceName,$serverName)).WaitForStatus(‘Running’,(new-timespan -seconds 10))
“$serviceName started”
}
else
{
“$serviceName is already $status”
}
}
else
{
“$serviceName not found in $serverName”
}
}
Call the method from you powershell scripts just that way
ManageRemoteService “MyServerPC” “MyAlertService” “Start”
ManageRemoteService “MyServerPC” “MyAlertService” “Stop”

Read Full Post »