Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

A Script to Generate A Script - Need Some Guidance

$
0
0

Hey All,

I have a little problem, mainly because my PowerShell skills are newbie at best, in getting my script to output to a new .ps1 file.

Basically, what I am trying to do is to capture the user inputs and form an actionable script that is saved and added to a scheduled Task in Windows 2012 R2. I have the scheduler function all set and would like a little help getting the script builder built.

Here's what I have so far:

function New-FileJobScript

{

Read-Host 'Welcome to the scheduled file transfer service.'

$Protocol = "None"

While ( $Protocol -eq "None" ) {

$RequestedProtocol = Read-Host  "Which protocol would you like to use? (FTP/FTPS/SFTP)"

Switch ( $RequestedProtocol.ToUpper() ) {

"FTP" { "You picked FTP" ; $Protocol = "FTP" }

"FTPS" { "You picked FTPS" ; $Protocol = "FTPS" }

"SFTP" { "You picked SFTP" ; $Protocol = "SFTP" }

default { "Default is FTP" ; $Protocol = "FTP"}

}

}

$Protocol

$Archive = "None"

While ( $Archive -eq "None" ) {

$RequestedArchive = Read-Host  "Would you like to have the transmitted file(s) archived?"

Switch ( $RequestedArchive.ToUpper() ) {

"Yes" { "You chose to archive the transmitted file." ; $Archive = "Yes" }

"No" { "You chose not to archive the transmitted file." ; $Archive = "No" }

default { "Default is Yes" ; $Archive = "Yes"}

}

}

if ( $Archive -eq "Yes" ) {

mkdir <ARCHIVEDIR>/$TaskName

$User = Read-Host 'What is the user name?'

$Pwd = Read-Host 'What is the password?'

$Server = Read-Host 'What is the server address?'

$NetworkDir = Read-Host 'Please input the network location of the file(s) you wish to send. Refer to documentation for more details.'

$RemoteDir = Read-Host 'Please input the REMOTE directory to which you will upload your files. If there is none please input a slash'

$Tmpl = 'winscp.com /command "option batch abort" "option confirm off" "open {0}://{1}:{2}@{3}" "put {4} {5}" exit'

$Command = $Tmpl -f  "$Protocol", "$User", "$Pwd", "$Server", "$NetworkDir", "$RemoteDir"

    }

}

#Run the function

New-FileJob

 

Previously I had the script pushing the created command into the task scheduler and dumping the output of $Command to the New-ScheduledTaskAction cmdlet. However the requirements got a little more complex, with the inclusion of an option to Archive the transmitted files. So I split the script into two functions, got the task creation function all set pretty easily, but hit a bit of a wall with the script creator.

Ultimately it needs to output a script that can just consist of the $Command output, but which becomes a touch more complex if Archiving is selected. I just can't seem to get the Out-file cmdlet put in there properly (I was piping the function to out-file but that obviously doesn't work because the script needs the file and directory created 1st).  

I could also use some pointers on how to get the Archive part to work, I feel like it's something simple but I'm just getting my feet wet with PowerShell and would love some pointers.

Thanks!!

Mike


Viewing all articles
Browse latest Browse all 6937

Trending Articles