Scheduled task to execute local script that has functions to execute a remote script
i want create script run every 3 or 4 hours. when executes, monitors user's mailbox new mail items containing "update available" in subject line. if find new mail item keywords, establishes connection remote server , executes update script located on remote server.
i've tried using invoke-command, doesn't seem work when try schedule script execute task. know of other methods this? below code have:
$dllpath = "c:\program files\microsoft\exchange\web services\1.1\microsoft.exchange.webservices.dll" [void][reflection.assembly]::loadfile($dllpath) $service = new-object microsoft.exchange.webservices.data.exchangeservice([microsoft.exchange.webservices.data.exchangeversion]::exchange2010_sp1) #get email address of user running function $windowsidentity = [system.security.principal.windowsidentity]::getcurrent() $sidbind = "ldap://<sid=" + $windowsidentity.user.value.tostring() + ">" $aceuser = [adsi]$sidbind $service.autodiscoverurl($aceuser.mail.tostring()) # find email message subject string $string = "brs (web) available!" $inbox = [microsoft.exchange.webservices.data.folder]::bind($service, [microsoft.exchange.webservices.data.wellknownfoldername]::inbox) # configure amount of items viewed $iv = new-object microsoft.exchange.webservices.data.itemview(1) # find email messages based on subject string $finditems = $service.finditems($folderid, $string, $iv) # create persistant connection remote computer $pass = convertto-securestring password -asplaintext -force $servcred = new-object -typename system.management.automation.pscredential -argumentlist user ,$pass $s = new-pssession -computername server -credential $servcred # if message containing $string has not been read yet, determine version number foreach ($item in $finditems.items){ if ($item.isread -eq $false){ $item.subject $item.datetimereceived $tostring = $item.subject # $tostring = brs (web) available! $emailversion = $tostring.tostring() # $emailversion = fw: v1190 brs (web) available! $emailversion = $emailversion.substring($emailversion.indexof("v")) # $emailversion = v1190 brs (web) available! $emailversion = $emailversion.substring(0,$emailversion.indexof(" ")) # $emailversion = v1190 $sftpversion = get-childitem u:\*.zip | sort-object lastwritetime -descending | select -first 1 $sftpversion = $sftpversion.tostring() # $sftpversion = u:\v1190.zip $sftpversion = $sftpversion.substring($sftpversion.indexof("v")) # $sftpversion = v1190.zip $sftpversion = $sftpversion.substring(0,$sftpversion.indexof(".")) # $sftpversion = v1190 if ($emailversion -ne $sftpversion){ invoke-command -session $s -scriptblock {c:\brsetup\update_now.ps1} -asjob } if($emailversion -eq $sftpversion){ write-host "sleeping..." -verbose # start-sleep 1800 } $item.isread = $true $item.update([microsoft.exchange.webservices.data.conflictresolutionmode]::alwaysoverwrite) } } get-job | stop-job -passthru
first of all, assume script works when run outside of scheduled task?
possibly long shot, since you're using
$pass = convertto-securestring password -asplaintext -force
you've generated encrypted version of password elsewhere insert script. have made sure generated encrypted string using same profile 1 being used in scheduled task?
when did similar found same issue, though in case further complicated fact calling script our monitoring system. created script spit out encrypted string text file, , found when ran ise got 1 output, when ran monitoring software got different one.
if you're unsure easiest way use :
$password = "<password>" $secure = convertto-securestring $password -force -asplaintext $bytes = convertfrom-securestring $secure $bytes | out-file c:\securepassword.txt
Windows Server > Windows PowerShell
Comments
Post a Comment