我有一个有效的PowerShell脚本。出于维护原因,我想创建另一个脚本,我将从第一个脚本中调用我将调用的所有参数。
如何创建和调用参数文件?
这是我的剧本:
param([string] $dataSource = "server")
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$Stamp1 = (Get-Date).toString("yyyy-MM-dd")
$Logfile = "E:\PowerShell\Log\file$stamp1.log"
$file = "file$stamp1.csv"
$extractFile = @"
E:\PowerShell\Output\$file
"@
[string]$sqlCommand1 =get-content -path E:\PowerShell\SQL\sql.sql
[string]$sqlCommand =$sqlCommand1
$authentication = ("User Id= user ;Password=pswd;" -f $plainCred.Username, $plainCred.Password)
Add-Type -assemblyname system.data
$factory = [System.Data.Common.DbProviderFactories]::GetFactory ("Teradata.Client.Provider")
$connection = $factory.CreateConnection()
$connection.ConnectionString = "Data Source = $dataSource;Connection Pooling Timeout=300;$authentication"
$connection.Open()
if ($connection.State -eq 'Open') {$logstring ="Connexion réussite"} else { $logstring ="echec Connexion" }
$command = $connection.CreateCommand()
$command.CommandText = $sqlCommand
$adapter = $factory.CreateDataAdapter()
$adapter.SelectCommand = $command
$dataset = new-object System.Data.DataSet
try
{
[void] $adapter.Fill($dataset)
$dataset.Tables | Select-Object -Expand Rows
}
finally
{
$connection.Close()
}
if (!$dataset) {$logstring1 ="extraction vide"} else {$logstring1 ="extraction réussite"}
($DataSet.Tables[0] | ConvertTo-Csv -delimiter ";" -NoTypeInformation ) -replace '"', "" | Out-File $extractFile -Force
$datafileExists = Test-Path $extractFile
if ($datafileExists)
{
$logstring2 ="Fichier data créé"
}
else
{
$logstring2 ="Fichier data non créé"
}
Add-content $Logfile -value ($Stamp+':'+$logstring)
Add-content $Logfile -value ($Stamp+':'+$logstring1)
Add-content $Logfile -value ($Stamp+':'+$logstring2)
我创建了一个参数文件
$Stamp1 = (Get-Date).toString("yyyy-MM-dd")
$Logfile = "E:\PowerShell\Log\file$stamp1.log"
$file = "file$stamp1.csv"
$extractFile = @"
E:\PowerShell\Output\$file
"@
$authentication = ("User Id= user ;Password=paswd;" -f $plainCred.Username, $plainCred.Password)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
我从我的第一个脚本中调用它:
$ScriptPath = Split-Path $MyInvocation.InvocationName
& "$ScriptPath\param.ps1"
但我的变量无法识别,我有这些错误:
Out-File : Cannot bind argument to parameter 'FilePath' because it is null. At E:\PowerShell\script\Soft.ps1:59 char:104 + ... "" | Out-File $extractFile -Force + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand Test-Path : Cannot bind argument to parameter 'Path' because it is null. At E:\PowerShell\script\Soft.ps1:61 char:29 + $datafileExists = Test-Path $extractFile + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand Add-Content : Cannot bind argument to parameter 'Path' because it is null. At E:\PowerShell\script\Soft.ps1:78 char:14 + Add-content $Logfile -value ($Stamp+':'+$logstring) + ~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-Content], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand Add-Content : Cannot bind argument to parameter 'Path' because it is null. At E:\PowerShell\script\Soft.ps1:79 char:13 + Add-content $Logfile -value ($Stamp+':'+$logstring1) + ~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-Content], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand Add-Content : Cannot bind argument to parameter 'Path' because it is null. At E:\PowerShell\script\Soft.ps1:80 char:13 + Add-content $Logfile -value ($Stamp+':'+$logstring2) + ~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-Content], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand
答案 0 :(得分:0)
如果您使用'&'执行ps1并且在该范围内可以创建创建的变量。你可以像这样改变范围:
df.loc[df['\tyear'] == 2000, 'rainfall (mm)'].sum()
更好的解决方案是使用'。'而不是''所以你从ps1获得的代码包含在另一个ps1中。
所以范围是相同的,因为它在同一个脚本中。