String 在powershell中合并到文件中(字符串和变量)

String 在powershell中合并到文件中(字符串和变量),string,function,variables,powershell,text-files,String,Function,Variables,Powershell,Text Files,我的问题是关于如何将一个字符串(包含$符号)和一个变量组合成一行写入文本文件。 我正在从一个函数创建一个ps1 函数pwd{ param( [参数(ValueFromPipeline=$True,position=0,mandatory=$True)][String]$Temp, [参数(ValueFromPipeline=$True,position=1,mandatory=$True)][String]$Path, [参数(ValueFromPipeline=$True,position=2

我的问题是关于如何将一个字符串(包含
$
符号)和一个变量组合成一行写入文本文件。
我正在从一个函数创建一个ps1

函数pwd{
param(
[参数(ValueFromPipeline=$True,position=0,mandatory=$True)][String]$Temp,
[参数(ValueFromPipeline=$True,position=1,mandatory=$True)][String]$Path,
[参数(ValueFromPipeline=$True,position=2,mandatory=$True)][String]$SubscriptionName
)
$NewFilePath=“$Path\newfile.ps1”
新建项-ItemType文件$NewFilePath-Force
添加内容$NewFilePath“$TimeStart=Get Date”
添加内容$NewFilePath“$AccountName=”“$($SubscriptionName)”””#这是我的疑问
}
$ScriptPath=拆分路径-父级$MyInvocation.MyCommand.Definition
pwdtest-Temp“String”-路径$ScriptPath-SubscriptionName”mail@something.com"
$ScriptPath=拆分路径-父级$MyInvocation.MyCommand.Definition
pwdtest-Temp“String”-路径$ScriptPath-SubscriptionName”mail@something.com"
这样调用函数
pwd

$ScriptPath=split path-parent$MyInvocation.MyCommand.Definition
pwd-Temp“String”-路径$ScriptPath-SubscriptionName”mail@something.com"
我怎样才能重写那句话

添加内容$NewFilePath“$AccountName=”“$($AutomationAccount)”
因为我需要在文本文件中写入以下内容:

$TimeStart=获取日期
$AccountName=”mail@something.com"
现在,如果我使用这一行,我在文本文件中有这样的内容:

$TimeStart=获取日期
="mail@something.com" 
如何说PowerShell
Add Content
不将
$AccountName
解释为“NULL”值,我需要将其用作内容字符串而不是变量

我有一个组合的想法:

addcontent$NewFilePath'$AccountName='+''“$($SubscriptionName)”“
但它不起作用

添加内容:找不到接受参数“+”的位置参数。
在C:\Users\JoseGabriel\test.ps1:10 char:5
+添加内容$NewFilePath“$AccountName=”+“”“$($SubscriptionName)”
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:InvalidArgument:(:)[Add Content],参数BindingException
+FullyQualifiedErrorId:PositionParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand
您是否尝试过:

添加内容$NewFilePath“`$TimeStart=Get Date”
添加内容$NewFilePath“`$AccountName=”“$($SubscriptionName)”“”
转义字符、分隔符和引号

PowerShell转义字符是严重重音(
`

转义字符有三种使用方式:

  • 当在行尾使用时,它是一个连续字符-因此命令将在下一行继续
  • 指示应在不替换的情况下传递后面的下一个字符。例如,
    $myVariable
    通常会展开以显示变量内容,但
    `$myVariable
    将作为
    $myVariable
    传递
  • 当在双引号内使用时,转义字符表示以下字符应解释为“特殊”字符

  • 资料来源:

    不,我对那个字符一无所知。非常感谢你,安斯加!成功了!好的,谢谢你的回答@MarcKellerman我对这个角色一无所知。