I started a few weeks ago on the PowerShell programing and I got my answer from here only. So am putting up the error that I am getting now to get some help again.
Scenario : I wrote a script that checks a Folder on a server and counts the number of files in it.Then when it finds the resulting number to be more then a specific value(threshold) it sends up a mail to the required alias. To break it all along the portion to send a mail is generated through DBmail using another .PS1 file.
In the PS1 file containing the main checking of number of files I am trying to pass 2 parameters :
1. Folder path on the server
2. the Threshold value of number of files
Now when I created a job to run this powershell script as :
& "C:\PowerShell\Power.PS1" \\XXXX\\c$\Documents and Settings\ABC\Local Settings\Temp 10
IT fails with the error :
A job step received an error at line 1 in a PowerShell script. The corresponding line is '& "C:\PowerShell\Power.ps1" \\XXXX\\c$\Documents and Settings\ABC\Local Settings\Temp 10'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot convert value "and" to type "System.Int32". Error: "Input string was not in a correct format." '. Process Exit Code -1. The step failed.
& "C:\PowerShell\Power.PS1" \\XXXX\\c$\Documents and Settings\ABC\Local Settings\Temp 10
The issue, as you know, is the & symbol. It was expecting a number, and not an & symbol. I cannot quite work out from this how you are starting this. Can you please show me how you are starting this script? It it from a .job in C:\Windows\Tasks?
I don't really understand this & symbol here to be quite honest.
1 x 640Gb (SATA 300)
Western Digital: WDC WD6400AAKS-75A7B0
1 x 1Tb (SATA 600)
Western Digital: Caviar Black, SATA 6GB/S, 64Mb cache, 8ms
Western Digital: WDC WD1002FAEX-00Z3A0 ATA Device
PSU
Stock PSU - 375W
Case
Dell XPS 420
Cooling
Stock Fan
Mouse
Advent Optical ADE-WG01 (colour change light up)
Keyboard
Dell Bluetooth
Internet Speed
120 kb/s
Other Info
ASUS USB 3.0 5Gbps/SATA 6Gbps - PCI-Express Combo Controller Card (U3S6)
I am actually doing this as a SQL Server Job with just one step in which I run the Power.Ps1 script and the script checks the number of files and then accordingly sends mail to the required alias.
Running the file from the Location "C:\PowerShell\power.ps1" from the local server and then passing the "\\XXXX\c$\Documents and Settings\ABC\Local Settings\Temp" as the path parameter or location where the script needs to check the number of files and passing "10" as the threshold number of files to decide which alert or mail to be sent.
To add I have taken the path mentioned on the server by connecting to it first and the Folder path mentioned is shared and the SQL server account running the services is the same account that has administration acccess on all servers.
I have even tried the code as :
& "C:\PowerShell\Power.PS1" $directory_string= "\\XXXX\c$\Documents and Settings\ABC\Local Settings\Temp" $benchmark= 10
Please bear with me! I am not very familiar with Powershell or SQL. I am certainly not the expert you really need, but am the only one on this forum who may be able to do something - taken from other programming languages. I don't mind if you re-post this at a Powershell forum, but I will do my best.
This is probably blindingly obvious to the knowledgable network admin you are and I am not, but the & is causing the errors. What purpose does it (try to) serve in the SQL initialisation of this PowerShell Script?
1 x 640Gb (SATA 300)
Western Digital: WDC WD6400AAKS-75A7B0
1 x 1Tb (SATA 600)
Western Digital: Caviar Black, SATA 6GB/S, 64Mb cache, 8ms
Western Digital: WDC WD1002FAEX-00Z3A0 ATA Device
PSU
Stock PSU - 375W
Case
Dell XPS 420
Cooling
Stock Fan
Mouse
Advent Optical ADE-WG01 (colour change light up)
Keyboard
Dell Bluetooth
Internet Speed
120 kb/s
Other Info
ASUS USB 3.0 5Gbps/SATA 6Gbps - PCI-Express Combo Controller Card (U3S6)
I really feel dum at time on this Blog, but I thought that I should share the details of resolution for this issue.
There was nothing wrong with the code that I had written to decide the Subject and the Body of the mail but was a problem with the script that I was using to send out mails using the DB mail feature in SQL Server. The small mistake that I was making was that I mentioned the name of the server and the database as :
abc.xyz
which in fact should have been [abc].[xyz]
This resolved the problem for me. Hope it helps others too.