passing file path as a parameter to .ps1 file

Hi 

passing file path as a parameter to .ps1 file 

for example: powershell ./test.ps1 -filepath 'D:\list'

I'm passing like this but its taking some other path.

how to fix this problem?

September 2nd, 2015 5:46am


Function send-Mail {
Param(
 #[Parameter(Mandatory=$True)]
 [string]$filepath 
     )
Function Format-FileSize() {
    Param ([int]$size)
    If     ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
    ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
    ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
    ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
    Else {[string]::Format("{0:0.00} kB", $size)}

#---------------------------------------------------------------------------

$smtpServer = "smtp.office365.com"
$smtpFrom = "xyz@mail.com"
$smtpTo = "xyz@mail.com"
$dte=(Get-Date).AddDays(-1).ToString('yyyy-MM-dd')
$messageSubject = "RECON :Files List $dte"

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px;background-color:thistle;}"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$ServerName= hostname
$glist = Get-ChildItem $filepath -Recurse

$message.Body = $glist  | Select-Object Name, CreationTime, @{Name="Size"; Expression={Format-FileSize($_.Length)}} | ConvertTo-Html  -Head $style
$message.Body = $message.Body + "<br>"
$message.Body = $message.Body + "List of files : $clist of $ServerName Server"


$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.EnableSsl = $True 
$smtp.Credentials = New-Object System.Net.NetworkCredential("MailID", "****Password****");
$smtp.Send($message)
}


Function NDCOsend-Mail {
Param(
 #[Parameter(Mandatory=$True)]
 [string]$filepath 
     )
$pqr=Get-ChildItem $filepath -Recurse
$qwe =$pqr|where {($_.Length)  -le 1kb}
if($qwe)
{
 #Send a mail to NDCO if file not received coreect or file size is less than 1 KB
#----------------------------------------------------------------------------------------
Function Format-FileSize() {
    Param ([int]$size)
    If     ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
    ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
    ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
    ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
    Else {[string]::Format("{0:0.00} kB", $size)} 
    }

$smtpServer = "smtp.office365.com"
$smtpFrom = "xyz@mail.com"
$smtpTo = "xyz@mail.com"
$dte=(Get-Date).AddDays(-1).ToString('yyyy-MM-dd')
$messageSubject = " FileList $dte"

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto

$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$ServerName= hostname

$message.Body = $qwe | Select-Object Name, CreationTime, @{Name="Size"; Expression={Format-FileSize($_.Length)}}| ConvertTo-Html  -Head $style
$message.Body = $message.Body + "<br>"
$message.Body = $message.Body + "Please check files and send it to  $ServerName server for the Bussines Date: " + $dte

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)

$smtp.EnableSsl = $True 

$smtp.Credentials = New-Object System.Net.NetworkCredential("MailID", "****Password****");

$smtp.Send($message)
 }
}



#Execute all tasks here 
#---------------------------------------------------------------------
workflow paralleltest {

    parallel 
    {

    if((Get-ChildItem 'D:\Feed\Import\ZIP' -Recurse -Include "*.ZIP","*.PDX").count -ge 8)
    {
    send-Mail
    }

    if((Get-ChildItem 'D:\Feed\Import\ZIP' -Recurse -Include "*.ZIP","*.PDX").count -ge 8)
    {
    NDCOsend-Mail
    }
 }
}

paralleltest

my script look like this and passing parameter to script like

powershell .\test.ps1 -filepath  'D:\Feed\Import\ZIP'

its taking some other folder information.

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 7:46am

The -filepath parameter is used in the functions, but you're not passing the parameter when you call them in the workflow.

http://blogs.technet.com/b/heyscriptingguy/archive/2013/01/30/powershell-workflows-using-parameters.aspx

September 2nd, 2015 7:57am

please anyone help me how to do with my code because i'm tried but not works.


Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 12:13pm

The article I posted has explicit instructions on how to add a parameter to workflows.
September 2nd, 2015 12:30pm

please anyone help me how to do with my code because i'm tried but not works.


Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 4:11pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics