Running a PowerShell script in SQL Server Agent is not working - powershell

I have a SQL Server Agent job that runs a PowerShell script to pull data from a database and save it in a csv file.
The job type is set to PowerShell and the command looks like this:
$sql=#'exec my_stored_procedure'#
$result = Invoke-Sqlcmd -ServerInstance INSTANCE_NAME -Database DBNAME -Query $sql
#The first row gets removed and then save it to a csv file
$result | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content \\SERVER\c$\Folder\file.csv
When I run this job, it runs successfully, but the file.csv is not created in the specified location.
I checked the Job History and it says:
The job script encountered the following errors. These error did not
stop the script: (.....) The corresponding line is $result |
ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content
\\SERVER\c$\Folder\file.csv. Correct the script and reschedule the
job. The error information returned by PowerShell is 'Cannot use
interface. The IContentCmdletProvider interface is not implemented by
this provider.'. Process Exit Code 0.
Could someone help me understand what I'm missing?
This is my first PowerShell project, so I'm probably missing things that are obvious to other people..?

It seems that it was complaining because of the server name in the path.
I originally had:
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
Set-Content \\SERVER\c$\Folder\file.csv
but CDing to the directory and specifying the file did the trick:
$directoryPath = "C:\Folder"
cd $directoryPath
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
Set-Content C:\Folder\file.csv

Related

How do I export these Active Directory User Properties to CSV? [duplicate]

I'm seriously sleep deprived and stressed so it is probably extremely simple yet I still can't manage to figure it out.
I want to get an export of various things like services, processes, firewallrules, local users, whatever I need as a CSV but whenever I export it as such the csv only has 1 column in Excel when I open it.
I just want the output to be separated by columns so I can have a simple overview in Excel.
Get-Service | Sort-Object Status | Format-Table -AutoSize | Out-String -Width 10000 | Out-File -Append "C:\temp\Test.csv"
But this is the output I get:
Services
This is what happens if I use a Export-CSV CMD: Services
PowerShell's default CSV delimiter is a comma. To make Excel read the CSV properly, change it to a semicolon using the -Delimiter parameter:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -Delim ";"
Or use the -UseCulture switch (thanks #JosefZ), which uses the same region-based ListSeperator-setting as Excel:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -UseCulture
Why not just export to csv then?
Get-Service | Select Name,DisplayName,Status | Sort-Object Status | Export-Csv "C:\temp\Test.csv" -NoTypeInformation

Query regarding the format of a csv file when using the export-csv command in powershell

I'm familiar with exporting data to csv with Powershell, but a particular vendor wants to receive the data in a file that is specific type 'CSV'. If I use the following example command, the data looks fine in the raw csv format (i.e via notepad), where you see it's comma separated ...
,,JVF1033292,test,SL,10700,6804626,745.586,43001843,Test,8/12/2020,8/14/2020,T,5584,,,JPY,0,XTKS,,,,,,0
,,JVF1033293,test,SL,3695,6805179,1362.8457,43001843,Test,8/12/2020,8/14/2020,T,3524,,,JPY,0,XTKS,,,,,,0
... but when the same file is opened in Excel all the data is in one row and therefore is failing on the vendors side. The code I'm using is below.
$Table | ConvertTo-Csv -NoTypeInformation | ForEach-Object {$_-replace "`"", ""} | select -skip 1 | out-file -filepath ("$dir_opr\LVT\Export\CTO\ForJefferies\GMO_Jefferies_Trade_Data_" + $Date+ ".csv")
If I use the below code, then it looks fine in Excel (tabbed correctly), but the raw file is also tabbed and not comma separated which the vendor has issues with.
$Table | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | ForEach-Object {$_-replace "`"", ""} | select -skip 1 | out-file -filepath ("$dir_opr\LVT\Export\CTO\ForJefferies\GMO_Jefferies_Trade_Data_" + $Date+ ".csv")
X JVF1032244 Test BY 450.0000 BYM41J3 10.00000000 43001843 Test 08/11/2020 08/13/2020 T 3.00 JPY 0 XTKS 0.00
X JVF1032245 Test BY 200.0000 BYM41J3 250.00000000 43001843 Test 08/11/2020 08/13/2020
Is it possible to create a comma separated and tabbed raw file, that also is delimited in Excel so not all in one column?
As far as I can tell with testing locally, it might be due to the default encoding that out-file uses - i.e utf8NoBOM. (See the description for the -Encoding parameter for details: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-7)
Try out-file -filepath $myfile -encoding utf8 - that seems to fix it on my machine.

Powershell Output File column too narrow

I am trying to list current installed applications and the DisplayVersion keeps getting its column shortened when I want it to show as normal on one line. It runs fine when running the code in a PS session but when running from a ps1 script file, the output for DisplayVersion is 1 character wide with NewLines for each integer.
$FormatEnumerationLimit = -1
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object Publisher, DisplayName, DisplayVersion |
Where {$_.DisplayName} |
Sort-Object -Property Publisher,DisplayName,DisplayVersion |
Format-Table -AutoSize -Wrap > $FullFileName
Considering the exact suggestion from LotPings, I am making this as an answer because it makes more sense to dump it in File using out-file & use Encoding instead of redirection.
Replace this:
Format-Table -AutoSize -Wrap > $FullFileName
To:
Out-File $FullFileName -Width 300 -Encoding Ascii

Formatting tables in PowerShell to export as CSV

I'm seriously sleep deprived and stressed so it is probably extremely simple yet I still can't manage to figure it out.
I want to get an export of various things like services, processes, firewallrules, local users, whatever I need as a CSV but whenever I export it as such the csv only has 1 column in Excel when I open it.
I just want the output to be separated by columns so I can have a simple overview in Excel.
Get-Service | Sort-Object Status | Format-Table -AutoSize | Out-String -Width 10000 | Out-File -Append "C:\temp\Test.csv"
But this is the output I get:
Services
This is what happens if I use a Export-CSV CMD: Services
PowerShell's default CSV delimiter is a comma. To make Excel read the CSV properly, change it to a semicolon using the -Delimiter parameter:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -Delim ";"
Or use the -UseCulture switch (thanks #JosefZ), which uses the same region-based ListSeperator-setting as Excel:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -UseCulture
Why not just export to csv then?
Get-Service | Select Name,DisplayName,Status | Sort-Object Status | Export-Csv "C:\temp\Test.csv" -NoTypeInformation

Powershell script to fetch task name from notepad and find the status of it in windows task scheduler

I need a PowerShell script where the tasks listed out in a text file needs to be read and the status of those tasks needs to be fetched from the windows task scheduler and output in to a csv file.
I am able to read the contents using Get-Content cmdlet.But Get-ScheduledTask is throwing an error that it is not recognized as a valid cmdlet.
Kindly help me with this as I am new to PowerShell.
Get-ScheduledTask tasks | Get-ScheduledTaskInfo | Select-Object TaskName, LastTaskResult | Export-Csv -NoTypeInformation -Path D:\First\one.csv
I have a txt file named tasks which contain the list of tasks to be iterated and listed out to a csv file
This one works for me:
$tasks = Get-Content -Path "C:\Users\Administrator\Desktop\Tasks.txt"
Get-ScheduledTask -TaskName $tasks | Get-ScheduledTaskInfo | Select-Object TaskName, LastTaskResult | Export-Csv C:\Users\Administrator\Desktop\Tasks.csv -NoTypeInformation
Txt file is in following format:
Task 1
Task 2
Task 3
I've tried it in PS 4 on Windows 8.1, you don't have this cmdlet in previous versions of Windows (like Windows 7 / Windows Server 2008 R2).
For those previous versions, you can use this great module. You should extract the entire folder in your Modules folder, e,g. to C:\Windows\System32\WindowsPowerShell\v1.0\Modules and then you can use Get-ScheduledTask from module like this:
Import-Module TaskScheduler
Get-Content -Path "C:\Users\Administrator\Desktop\Tasks.txt" | ForEach-Object { Get-ScheduledTask -Name $_ | Select-Object Name,LastRunTime } | Export-Csv C:\Users\Administrator\Desktop\Tasks.csv -NoTypeInformation
Other solution would be to try to play around with schtasks.exe or the Schedule.Service COM object but I think the module TaskScheduler is easier for you.