Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I would like to write a powershell script that gets the following parameters as input:
Folder to copy from, extensions allows, folder to copy to and a boolean indicating if the change should restart IIS, username and password.
What cmdlets should I be looking at considering that I am copying to a remote server?
How do I read the parameters into variables?
How do I restart IIS?

Cosidering that I might want to copy multiple folders, how do I write a powershell script that invokes a powershell script?

share|improve this question
    
Wow, sorry for a delay in any response... Do you still need help? –  Marco Shaw Dec 18 '10 at 21:49
    
Yes, I don't really know how to restart IIS? How do I make sure the files are really copied? –  the_drow Dec 18 '10 at 23:20

3 Answers 3

up vote 10 down vote accepted

Get-ChildItem allows you to list files and directories, including recursively with filename filters. Copy-Item allows you to copy a file.

There is a lot of overlap in terms of selecting the files, often Copy-Item on its own is sufficient depending on the details of what you need (eg. do you want to retain the folder structure?)

To copy all *.foo and *.bar from StartFolder to DestFolder:

Copy-Item -path "StartFolder" -include "*.foo","*.bar" -Destination "DestFolder"

If you need to preserve the folder structure things get harder because you need to build the destination folder name, something like:

Get-ChildItem -path "StartFolder" -recurse -include "*.foo","*.bar" |
  Foreach-Object { Copy-Item -path $_ -destination { $_.FullName -replace "StartFolder","DestFolder"}}

But robocopy is likely to be easier:

robocopy StartFolder DestFolder *.foo *.bar /s

In the end the way to choose will depend on the details of what's needed.

(In the above I've avoided aliases (e.g. Copy-Item rather than copy) and explicitly use parameter names even if they are positional.)

share|improve this answer

I can't address the IIS portion, but the file copy while preserving the directory structure can be a lot simpler than shown in the other answers:

Copy-Item -path "StartFolder" -Recurse -Include "*.foo","*.bar" -Destination "DestFolder" -Container

The -Container argument is the magic part that will replicate the structure in the destination as it is in the source.

share|improve this answer

If you are using IIS7, you can use native component to replicate content between IIS servers: http://blog.theplanet.com/2010/05/18/mirroring-server-content-and-configuration-in-iis7/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.