I am running a custom PowerShell Script in Power Automate desktop
this is the script
# Get the 1st day of the current month
$now_date = Get-Date -Day 1 -Month (Get-Date).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
# Get the last day of the current month
$month_after = (Get-Date -Day 1 -Month (Get-Date).Month -Year (Get-Date).Year).AddMonths(1).AddDays(-1) -Format "dd-MMM-yy"
# Initialize variables for the previous month's 1st and last days
$prev_month_first = $null
$prev_month_last = $null
# Check if now_date and month_after are the same
if ($now_date -eq $month_after) {
# Calculate the 1st day of the previous month
$prev_month_first = (Get-Date -Day 1 -Month (Get-Date).AddMonths(-1).Month -Year (Get-Date).AddMonths(-1).Year) -Format "dd-MMM-yy"
# Calculate the last day of the previous month
$prev_month_last = (Get-Date -Day 1 -Month (Get-Date).Month -Year (Get-Date).Year).AddDays(-1) -Format "dd-MMM-yy"
}
# Output the values
$now_date, $month_after, $prev_month_first, $prev_month_last
Now when I set a new variable in power automate desktop I cant get the output of my 4 variables here
I set a new variable name it to "Now_Date" and in value I put %now_date%
The output that the script gives me is named as %PowershellOutput%
but the result is always empty I don’t mind getting the result in text format as well because I have to copy paste it somewhere
Please can someone help with this
Your Script produces errors and thus returns a blank.
Enable the ScriptError
variable under the variables produced options.
There are other errors in your script:
The script should be this:
# Get the 1st day of the current month
$now_date = Get-Date -Day 1 -Month (Get-Date).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
# Get the last day of the current month
$month_after = Get-Date -Day 1 -Month (Get-Date).AddMonths(1).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
# Initialize variables for the previous month's 1st and last days
$prev_month_first = $null
$prev_month_last = $null
# Check if now_date and month_after are the same
if ($now_date -eq $month_after) {
# Calculate the 1st day of the previous month
$prev_month_first = Get-Date -Day 1 -Month (Get-Date).AddMonths(-1).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
#calculate last day of month
$last_days_in_month = [DateTime]::DaysInMonth((Get-Date).Year, (Get-Date).AddMonths(-1).Month)
# Calculate the last day of the previous month
$prev_month_last = Date -Day $last_days_in_month -Month (Get-Date).AddMonths(-1).Month -Year (Get-Date).Year -Format "dd-MMM-yy"
}
# Output the values
$now_date, $month_after, $prev_month_first, $prev_month_last
Get your script to run correctly in PowerShell, when I tested your script in Windows PowerShell ISE I got the same errors.
Once your script passes you will see the output in %PowershellOutput%
this will be a multiline string, so you could use the Split Text action in power automate desktop to get a list, after which you can get the values according to the index in TextList
variable.
copy and then paste this text to a power automate desktop
Text.SplitText.SplitWithDelimiter Text: PowershellOutput CustomDelimiter: $'''\\n''' IsRegEx: True Result=> TextList
You should get something like this
Get the values using the index position, so %TextList[1]%
will give you the string calculated by $month_after
.
If you wanted to return everything as a single string then you can write your last line something like
Write-Host "$($now_date), $($month_after) ...etc"