Comments on: scheduling powershell tasks with sql agent https://dbatools.io/agent/ the community's sql powershell module Thu, 23 Apr 2020 17:15:17 +0000 hourly 1 https://wordpress.org/?v=5.3.4 By: Peter Daniels https://dbatools.io/agent/#comment-80295 Thu, 23 Apr 2020 17:15:17 +0000 https://dbatools.io/?p=5786#comment-80295 @wsuhoey and I just worked through an issue with $env:PSModulePath on slack (Thank you, @wsuhoey). Even though I was using the same domain account to develop my script (in vscode) and to run the SQL Agent proxy for cmdExec, it was not using the same profiles, and therefore an Import-Module in my script quietly failed in my job exec even with -Verbose and -ErrorAction “Stop”. Just a note for folks to pay attention to various powershell profiles and $env:PSModulePath. Cheers.

]]>
By: vishvas https://dbatools.io/agent/#comment-76768 Thu, 14 Nov 2019 07:52:40 +0000 https://dbatools.io/?p=5786#comment-76768 HI

Below script is working fine in ISE but if i run the same script in agent job iam getting blank output, can you please look into it and suggest me what changes i need to make

int] $Threshold = 20;
[Object[]] $computers = get-content -Path “”;
[string] $messagebody = “Server Name:” +$computer+”
Disk Name:” +$deviceid +”
Free Space Available:” +$freeSpaceGB +” GB

Recomendation-Either shrink the file or add more disk space to the server”;
[string] $smtpAddress = “”;
[string] $toAddress = “”;
[string] $fromAddress = “”;
foreach($computer in $computers){
$disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter “DriveType = 3”;
$computer = $computer.toupper();
$deviceid = $disk.DeviceID;
foreach($disk in $disks){
$freeSpaceGB = [Math]::Round([float]$disk.FreeSpace/1073741824, 2);
if($freeSpaceGB -lt $Threshold){
[string] $username = “”;
[string] $password= “”;
[SecureString] $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force;
[PSCredential] $mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd);
[string] $subject = “Warning Diskspace below the threshold”+ $computer +”for”+ $deviceid +”drive”;
Send-MailMessage -From $fromAddress -to $toAddress -Subject $subject -body $messagebody -smtpserver $smtpAddress -UseSsl -Port 25 -Credential $mycreds;
}
}
}

]]>
By: Chrissy LeMaire https://dbatools.io/agent/#comment-73177 Wed, 04 Sep 2019 11:18:12 +0000 https://dbatools.io/?p=5786#comment-73177 Hey Adrian, yours is the second report and have no idea how to fix it, unfortunately. It’s one of the most challenging issues as we cannot reproduce it. Perhaps you can -ErrorAction Ignore since it continues to work.

]]>
By: Adrian https://dbatools.io/agent/#comment-73171 Wed, 04 Sep 2019 10:33:00 +0000 https://dbatools.io/?p=5786#comment-73171 Hi, I have a ps1 file that startswith “import-module dbatools”, which works fine interactively
In the agent job it returns…
Message
Executed as user: domain\myUser. Join-Path : Cannot bind argument to parameter ‘Path’ because it is null. At C:\Program Files\WindowsPowerShell\Modules\dbatools\1.0.38\internal\configur ations\configuration.ps1:72 char:44 + $script:path_FileUserLocal = Join-Path $Env:LocalAppData “$psVers … + ~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingVa lidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M icrosoft.PowerShell.Commands.JoinPathCommandJoin-DbaPath : Cannot bind argument to parameter ‘Path’ because it is an empty string. At C:\Program Files\WindowsPowerShell\Modules\dbatools\1.0.38\internal\configur ations\configuration.ps1:90 char:48 + $script:path_FileUserShared = Join-DbaPath $Env:AppData $psVersio … + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-DbaPath], ParameterBindin gValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl lowed,Join-DbaPath. Process Exit Code 0. The step succeeded.
– however the script continues and the tools appear to work ok…

]]>
By: dbatools advanced configuration – dbatools https://dbatools.io/agent/#comment-72227 Tue, 20 Aug 2019 13:15:46 +0000 https://dbatools.io/?p=5786#comment-72227 […] settings are scoped at the session-level. This means that if you use dbatools module within SQL Agent jobs and you have a different account to run it, you will need to set the options for the […]

]]>
By: Chrissy LeMaire https://dbatools.io/agent/#comment-66390 Wed, 29 May 2019 20:06:48 +0000 https://dbatools.io/?p=5786#comment-66390 Hey Tim, can you check out this post and see if it helps and let us know? https://github.com/dataplat/dbatools/issues/5640

]]>
By: Tim Toennies https://dbatools.io/agent/#comment-66388 Wed, 29 May 2019 20:02:30 +0000 https://dbatools.io/?p=5786#comment-66388 I had been using Invoke-DBAQuery in a SQL Agent job for the last few months without an incident but… last week I started receiving an error when I ran a scheduled task which included Invoke-DBAQuery. It runs successfully when I run it manually. I’ve reinstalled the module on numerous occasions using run as administrator. Here is the error.
Date 5/29/2019 1:11:10 PM
Log Job History (DBA – Get SQL Server data)

Step ID 1
Server DSQLB8SM001
Job Name DBA – Get SQL Server data
Step Name Launch PowerShell script
Duration 00:00:04
Sql Severity 0
Sql Message ID 0
Operator Emailed
Operator Net sent
Operator Paged
Retries Attempted 0

Message
Invoke-DbaQuery : The ‘Invoke-DbaQuery’ command was found in the module
‘dbatools’, but the module could not be loaded. For more information, run
‘Import-Module dbatools’.
At C:\scripts\powershell\Get-SQLServerData.ps1:56 char:13
+ $SQLServers=Invoke-DbaQuery -SqlInstance dsqlb8sm001 -Query “SELECT …
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-DbaQuery:String) [], Par
entContainsErrorRecordException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

Any suggestions?

]]>
By: Export SQL Server Agent Jobs #Dbatools | Reginaldo Silva https://dbatools.io/agent/#comment-50797 Tue, 19 Feb 2019 03:39:44 +0000 https://dbatools.io/?p=5786#comment-50797 […] https://dbatools.io/agent/ […]

]]>
By: Serge M https://dbatools.io/agent/#comment-45942 Thu, 31 Jan 2019 20:46:14 +0000 https://dbatools.io/?p=5786#comment-45942 Hi Chrissy,

Thanks for sharing such a useful and detailed piece of information on scheduling PowerShell tasks with SQL Agent.
One little thing that is missing is the necessity of using -NoPorfile switch.
powershell.exe -NoProfile -File \\location\to\file.ps1
Otherwise, some of the cmdlets may fail. In particular, when calling Get-DbaBuildReference, we were getting the following error until we added the switch:
Error loading SQL build reference | Cannot bind argument to parameter ‘Path’ because it is null.

]]>
By: Em desenvolvimento https://dbatools.io/agent/#comment-33496 Mon, 10 Dec 2018 11:00:33 +0000 https://dbatools.io/?p=5786#comment-33496 […] Não irei reinventar a roda. Aqui está passo a passo com detalhes: https://dbatools.io/agent/. […]

]]>