I have the following code:
<code>param (
[Parameter(Mandatory)][string]$DummyOU, # OU to put dummy devices in.
[Parameter(Mandatory)][string]$CertTemplate, # CA Certificate template short name used when issuing certs to intune devices.
[string]$logfile = "$([System.IO.Path]::GetFileNameWithoutExtension("$($PSCommandPath | Split-Path -Leaf)"))-log.csv"
)
"$($PSCommandPath | Split-Path -Leaf)" | Out-File C:Automationtest.txt
"$([System.IO.Path]::GetFileNameWithoutExtension("$($PSCommandPath | Split-Path -Leaf)"))-log.csv" | Out-File C:Automationtest.txt -Append
"$logfile" | Out-File C:Automationtest.txt -Append
exit 0
</code>
<code>param (
[Parameter(Mandatory)][string]$DummyOU, # OU to put dummy devices in.
[Parameter(Mandatory)][string]$CertTemplate, # CA Certificate template short name used when issuing certs to intune devices.
[string]$logfile = "$([System.IO.Path]::GetFileNameWithoutExtension("$($PSCommandPath | Split-Path -Leaf)"))-log.csv"
)
"$($PSCommandPath | Split-Path -Leaf)" | Out-File C:Automationtest.txt
"$([System.IO.Path]::GetFileNameWithoutExtension("$($PSCommandPath | Split-Path -Leaf)"))-log.csv" | Out-File C:Automationtest.txt -Append
"$logfile" | Out-File C:Automationtest.txt -Append
exit 0
</code>
param (
[Parameter(Mandatory)][string]$DummyOU, # OU to put dummy devices in.
[Parameter(Mandatory)][string]$CertTemplate, # CA Certificate template short name used when issuing certs to intune devices.
[string]$logfile = "$([System.IO.Path]::GetFileNameWithoutExtension("$($PSCommandPath | Split-Path -Leaf)"))-log.csv"
)
"$($PSCommandPath | Split-Path -Leaf)" | Out-File C:Automationtest.txt
"$([System.IO.Path]::GetFileNameWithoutExtension("$($PSCommandPath | Split-Path -Leaf)"))-log.csv" | Out-File C:Automationtest.txt -Append
"$logfile" | Out-File C:Automationtest.txt -Append
exit 0
It produces the following output in test.txt
:
<code>map-intune-certs.ps1
map-intune-certs-log.csv
-log.csv
</code>
<code>map-intune-certs.ps1
map-intune-certs-log.csv
-log.csv
</code>
map-intune-certs.ps1
map-intune-certs-log.csv
-log.csv
I execute the script with the following command line:
<code>powershell.exe -ExecutionPolicy Bypass -File .map-intune-certs.ps1 -DummyOU "Test" -CertTemplate "Test2"
</code>
<code>powershell.exe -ExecutionPolicy Bypass -File .map-intune-certs.ps1 -DummyOU "Test" -CertTemplate "Test2"
</code>
powershell.exe -ExecutionPolicy Bypass -File .map-intune-certs.ps1 -DummyOU "Test" -CertTemplate "Test2"
Notice that $logfile
is corrupted.
However, this problem does not occur if I remove the mandatory parameters. It also appears the problem does not occur in Powershell v7.4.3 whereas it does occur with v5.1.22621.3880. Why does $([System.IO.Path]::GetFileNameWithoutExtension("$($PSCommandPath | Split-Path -Leaf)"))
produce blank output when used in the param
block along with the mandatory parameters?