HiveBrain v1.2.0
Get Started
← Back to all entries
snippetshellMinor

Powershell script to create folders on remote servers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptcreateserverspowershellfoldersremote

Problem

This is pretty much the first Powershell script I've ever done and I am here looking for a quick review of it. Is there something I could have done better? Is the format okay? I don't know much about expected standards, and this was cobbled together as a result of me wanting to learn.

$Company = Read-Host 'Enter Company Name:'
$Credentials = Get-Credential

Write-Host 'Thank you! Creating client FTP folder on ServerA.' -ForegroundColor Magenta -BackgroundColor White 

Invoke-Command ServerA {mkdir ("e:\ftp\users\" + $args[0] + "\INCOMING")} -ArgumentList $Company -Credential $Credentials
Invoke-Command ServerA {mkdir ("e:\ftp\users\" + $args[0] + "\OUTGOING")} -ArgumentList $Company -Credential $Credentials

Write-Host 'Done. Now creating duplicate folder on ServerB.' -ForegroundColor Magenta -BackgroundColor White 

Invoke-Command ServerB {mkdir ("e:\ftp\users\" + $args[0] + "\INCOMING")} -ArgumentList $Company -Credential $Credentials
Invoke-Command ServerB {mkdir ("e:\ftp\users\" + $args[0] + "\OUTGOING")} -ArgumentList $Company -Credential $Credentials

Write-Host 'Done.' -ForegroundColor Magenta -BackgroundColor White


Whenever we first set up a user for FTP, we have to set up a folder for them. The folder needs to be named after the company, and include "Incoming" and "Outgoing" subfolders. This needs to be created on ServerA, and the exact same thing created on ServerB. This script finds out what the company name is, checks credentials, and then creates the folders in the appropriate places.

Thoughts? Things I could research to improve it?

Solution

Congratulations, you managed to get a PS script to run, that's already something!

Jokes aside, you should look into parameterizing the script, so you could invoke it with command-line arguments instead of prompting for them, if that's possible. This would also reduce the maintenance burden on the script, because if the "server A" and "server B" aren't hard-coded in the script, there's nothing to change but how the script is invoked when either server needs to be changed.

Something like this at the start of the script:

param (
    $Company = "Contoso Inc."
    $ServerA = "PathA"
    $ServerB = "PathB"
)


There's no need for Write-Host, if you want to output a string, just output a string:

"Some output"


Will output the string... Some output. Yup. That easy.

Code Snippets

param (
    $Company = "Contoso Inc."
    $ServerA = "PathA"
    $ServerB = "PathB"
)
"Some output"

Context

StackExchange Code Review Q#77741, answer score: 4

Revisions (0)

No revisions yet.