Forum Discussion
PowerShell doens't run correctly from scheduled task Job
- Apr 03, 2024I guess adding import-module PnP.Powershell should fix that. (Add it as the second line in the script after the Start-Transcript)
Hi Harm_Veenstra ,
Thanks for the input shared, I have tried to update the code with Start-Transcript c:\temp\log.txt at the top and a Stop-Transcript at th ebottom and when I run the script manually it creates the log file but when I run it in scheduled task does not create the file.
Microsoft Windows 10 Enterprise
Scheduled Task -
Program/Script: powershell.exe
Arguments: C:\Users\usernameLeave_extraction_script.ps1'
Do you run it as System or a user? Does that user have access to the folder in which the script is located?
- GNavyaApr 02, 2024Copper Contributor
Hi Harm_Veenstra ,
Have updated the schedule task but still the log file doesn't gets updated..It is an empty file.
But when I run it manually the log files gets updated..gets all the details and updates in the Sharepoint online calendar.
The task runs with a user account and yes, the folder containing the script has user access.
But have observed one thing when the schedule job runs a blue screen gets opened and a line appear and immediately it gets disappeared..Unable to view the message provided in it.
Below is the updated code with transcript command:# Install the SharePoint PnP PowerShell <module if not already installed>
# Install-Module -Name PnP.PowerShell -Force -AllowClobber
#Install-Module -Name PnP.Powershell#Import-Module "C:\Users\pnp_3.29\SharePointPnPPowerShellOnline.psd1" -DisableNameChecking
# Connect to the SharePoint siteStart-Transcript C:\temp\log.txt
$siteUrl = "<URL>"
$siteUrlDestination = "<URL>"
# Specify Calendar list name
$listName = "Calendar"
$listNameDestination = "CalenderNew"
$ResultColl = @()
$cred = Import-Clixml -Path "C:\Users\creds.xml"
Connect-PnPOnline -Url $siteUrl -Credentials $cred
# Retrieve items from the Calendar list
$calendarData = Get-PnPListItem -List $listName
#Get All Field Values from the List Item
$FieldValuescol = $calendarData.FieldValues
foreach ($FieldValues in $FieldValuescol)
{
$Result = new-object PSObject
$Result | add-member -membertype NoteProperty -name "Title" -Value $FieldValues.Title
$Result | add-member -membertype NoteProperty -name "Type Of Leave" -Value $FieldValues.Type_x0020_Of_x0020_Leave
$Result | add-member -membertype NoteProperty -name "All Day Event" -Value $FieldValues.fAllDayEvent
$Result | add-member -membertype NoteProperty -name "Start Time" -Value $FieldValues.EventDate
$Result | add-member -membertype NoteProperty -name "EndTime" -Value $FieldValues.EndDate
$Result | add-member -membertype NoteProperty -name "Half Day" -Value $FieldValues.Half_x0020_Day
$Result | add-member -membertype NoteProperty -name "Number of Working Days" -Value $FieldValues.Number_x0020_of_x0020_Working_x0
$Result | add-member -membertype NoteProperty -name "Description" -Value $FieldValues.Description
$Result | add-member -membertype NoteProperty -name "Team" -Value $FieldValues.Team
$ResultColl += $Result
}
#$ResultColl #| Export-Csv $outputPath -NoTypeInformation
# Disconnect from the SharePoint site
Disconnect-PnPOnline
#Destination
Connect-PnPOnline -Url $siteUrlDestination -UseWebLogin
#remove all existing items
# Get all items from the list
$items = Get-PnPListItem -List $listNameDestination
# Loop through each item and delete it
foreach ($item in $items) {
Remove-PnPListItem -List $listNameDestination -Identity $item.Id -Force
}
#refresh the list
foreach ($res in $ResultColl)
{
$itemProperties = @{
Title = $res.Title
Type_x0020_Of_x0020_Leave = $res.'Type Of Leave'
fAllDayEvent = $res.'All Day Event'
EventDate = $res.'Start Time'
EndDate = $res.'EndTime'
Half_x0020_Day = $res.'Half Day'
Number_x0020_of_x0020_Working_x0 = $res.'Number of Working Days'
Description = $res.Description
Team = $res.Team
}
Add-PnPListItem -List $listNameDestination -Values $itemProperties
}
Disconnect-PnPOnlineStop-Transcript
- Apr 02, 2024
GNavya Seems to work fine, except for all the files that I don't have like the credentials XML file, with these settings:
Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments: C:\Users\Harm\test.ps1
(Change the ps1 path 😉 )
- GNavyaApr 03, 2024Copper Contributor
Hi Harm_Veenstra ,
Thanks and Appreciate your quick resopnse.
I have tried to change the ps1 script path and run the job. Now I see the log file getting created but it throws errors.
**********************
Windows PowerShell transcript start
Start time: 20240403080714
Username: navya
RunAs User: navya
Configuration Name:
Machine: <Servername> (Microsoft Windows NT 10.0.19045.0)
Host Application: powershell.exe C:\Users\Calendar\Leave_extraction_script.ps1
Process ID: 24500
PSVersion: 5.1.19041.4170
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.4170
BuildVersion: 10.0.19041.4170
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\temp\log.txt
Connect-PnPOnline : The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Calendar\Leave_extraction_script.ps1:13 char:1
+ Connect-PnPOnline -Url $siteUrl -Credentials $cred
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Connect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Connect-PnPOnline : The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\Calendar\Leave_extraction_script.ps1:13 char:1
+ Connect-PnPOnline -Url $siteUrl -Credentials $cred
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Connect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundExceptionGet-PnPListItem : The term 'Get-PnPListItem' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\Calendar\Leave_extraction_script.ps1:17 char:17
+ $calendarData = Get-PnPListItem -List $listName
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-PnPListItem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Get-PnPListItem : The term 'Get-PnPListItem' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\Calendar\Leave_extraction_script.ps1:17 char:17
+ $calendarData = Get-PnPListItem -List $listName
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-PnPListItem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundExceptionDisconnect-PnPOnline : The term 'Disconnect-PnPOnline' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At C:\Users\Calendar\Leave_extraction_script.ps1:39 char:1
+ Disconnect-PnPOnline
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Disconnect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Disconnect-PnPOnline : The term 'Disconnect-PnPOnline' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At C:\Users\Calendar\Leave_extraction_script.ps1:39 char:1
+ Disconnect-PnPOnline
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Disconnect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundExceptionConnect-PnPOnline : The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\Calendar\Leave_extraction_script.ps1:43 char:1
+ Connect-PnPOnline -Url $siteUrlDestination -UseWebLogin
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Connect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Connect-PnPOnline : The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\Calendar\Leave_extraction_script.ps1:43 char:1
+ Connect-PnPOnline -Url $siteUrlDestination -UseWebLogin
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Connect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundExceptionGet-PnPListItem : The term 'Get-PnPListItem' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\Calendar\Leave_extraction_script.ps1:47 char:10
+ $items = Get-PnPListItem -List $listNameDestination
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-PnPListItem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Get-PnPListItem : The term 'Get-PnPListItem' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\Calendar\Leave_extraction_script.ps1:47 char:10
+ $items = Get-PnPListItem -List $listNameDestination
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-PnPListItem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundExceptionDisconnect-PnPOnline : The term 'Disconnect-PnPOnline' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At C:\Users\Calendar\Leave_extraction_script.ps1:69 char:1
+ Disconnect-PnPOnline
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Disconnect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Disconnect-PnPOnline : The term 'Disconnect-PnPOnline' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At C:\Users\Calendar\Leave_extraction_script.ps1:69 char:1
+ Disconnect-PnPOnline
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Disconnect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException**********************
Windows PowerShell transcript end
End time: 20240403080716
**********************I also see the popup coming as below. Have added the -Scope CurrentUser" for the command in the script - (Install-Module -Name PnP.PowerShell -Force -AllowClobber -Scope CurrentUser) and ran it still the same errors in the log file.
I can only run the script with useraccount.
Regards,
Navya