My question is related about how can I write a line that combines a string (that contains a $
symbol) and a variable into a single line that goes into text file.
I'm creating a ps1 from a function.
function pwd{
param(
[Parameter(ValueFromPipeline=$True,position=0,mandatory=$true)][String]$Temp,
[Parameter(ValueFromPipeline=$True,position=1,mandatory=$true)][String]$Path,
[Parameter(ValueFromPipeline=$True,position=2,mandatory=$true)][String]$SubscriptionName
)
$NewFilePath = "$Path\newfile.ps1"
New-Item -ItemType file $NewFilePath -Force
Add-Content $NewFilePath ' $TimeStart=Get-Date'
Add-Content $NewFilePath " $AccountName=""$($SubscriptionName)"" " #Here's my doubt
}
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
pwdtest -Temp "String" -Path $ScriptPath -SubscriptionName "mail@something.com"
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
pwdtest -Temp "String" -Path $ScriptPath -SubscriptionName "mail@something.com"
So calling the function pwd
this way:
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
pwd -Temp "String" -Path $ScriptPath -SubscriptionName "mail@something.com"
How can I rewrite that line?
Add-Content $NewFilePath " $AccountName=""$($AutomationAccount)"" "
Because I need to write in the text file this:
$TimeStart=Get-Date
$AccountName = "mail@something.com"
And right now if I use that line I've got this in the text file:
$TimeStart=Get-Date
="mail@something.com"
How can I say PowerShell Add-Content
not interpretate the $AccountName
as a "NULL" value and I need to use it as a content string and not a variable.
I had this idea of combination:
Add-Content $NewFilePath ' $AccountName=' + " ""$($SubscriptionName)"" "
But It doesn't work.
Add-Content : A positional parameter cannot be found that accepts argument '+'.
At C:\Users\JoseGabriel\test.ps1:10 char:5
+ Add-Content $NewFilePath ' $AccountName=' + " ""$($SubscriptionName)"" "
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-Content], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand
Have you tried:
Add-Content $NewFilePath " `$TimeStart=Get-Date"
Add-Content $NewFilePath " `$AccountName=""$($SubscriptionName)"""
Escape characters, Delimiters and Quotes
The PowerShell escape character is the grave-accent( `
).
The escape character can be used in three ways:
$myVariable
will normally be expanded to display the variables contents but `$myVariable
will just be passed as $myVariable
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.