Whenever creating an FTP folder for an FTP client in my company, I have to create two folders on two servers; server A and server B. I've tried to give an example below.
E:\FTP\CompanyName
E:\FTP\CompanyName\Incoming
E:\FTP\CompanyName\Outgoing
It would need to look like this on both servers. It isn't that hard to do manually, but I am wanting to learn Powershell, so why not automate a simple task? Here is what I currently have:
$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" + $Company + "\INCOMING")} -Credential $Credentials
Invoke-Command ServerA {mkdir ("e:\ftp" + $Company + "\OUTGOING")} -Credential $Credentials
Write-Host 'Done. Now creating duplicate folder on ServerB.' -ForegroundColor Magenta -BackgroundColor White
Invoke-Command ServerB {mkdir e:\ftp\$Company\INCOMING} -Credential $Credentials
Invoke-Command ServerB {mkdir e:\ftp\$Company\OUTGOING} -Credential $Credentials
Write-Host 'Done.' -ForegroundColor Magenta -BackgroundColor White
We have a set of admin credentials that we use specifically for server access. The script asks for those credentials and then also asks the "CompanyName" is. In theory, I would like it to then apply that variable to the "CompanyName" section of the file path.
I tried it two different ways in the script, just to test. They both completely skip the variable and just create an incoming and outgoing folder in E:\ftp. What am I missing here?
Any other advice on how to clean up my script is welcome as well. I've learned a lot doing this, but there is quite a bit more to learn.
The variable $company
does not exist in the session on the other server so it can´t be used to create the folder like you want.
You can pass your variable by using the -Argumentlist
parameter of Invoke-Command
like this:
Invoke-Command ServerA {mkdir ("e:\ftp" + $args[0] + "\INCOMING")} -ArgumentList $Company -Credential $Credentials