使用PowerShell创建要由另一个脚本调用的参数脚本
我有一个有效的PowerShell脚本。出于维护的原因,我想创建另一个脚本,在其中放置第一个脚本中调用的所有参数 如何创建和调用参数文件 这是我的脚本:使用PowerShell创建要由另一个脚本调用的参数脚本,powershell,Powershell,我有一个有效的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$st
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
Out File:无法将参数绑定到参数“FilePath”,因为它为空。
在E:\PowerShell\script\Soft.ps1:59 char:104
+ ... “”|输出文件$extractFile-强制
+ ~~~~~~~~~~~~
+CategoryInfo:InvalidData:(:)[Out File],参数BindingValidationException
+FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand
测试路径:无法将参数绑定到参数“Path”,因为它为null。
在E:\PowerShell\script\Soft.ps1:61 char:29
+$datafileExists=测试路径$extractFile
+ ~~~~~~~~~~~~
+CategoryInfo:InvalidData:(:)[Test Path],ParameterBindingValidationException
+FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand
添加内容:无法将参数绑定到参数“Path”,因为它为null。
在E:\PowerShell\script\Soft.ps1:78 char:14
+添加内容$Logfile-value($Stamp+':'+$logstring)
+ ~~~~~~~~
+CategoryInfo:InvalidData:(:)[Add Content],ParameterBindingValidationException
+FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand
添加内容:无法将参数绑定到参数“Path”,因为它为null。
在E:\PowerShell\script\Soft.ps1:79 char:13
+添加内容$Logfile-value($Stamp+':'+$logstring1)
+ ~~~~~~~~
+CategoryInfo:InvalidData:(:)[Add Content],ParameterBindingValidationException
+FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand
添加内容:无法将参数绑定到参数“Path”,因为它为null。
在E:\PowerShell\script\Soft.ps1:80 char:13
+添加内容$Logfile-value($Stamp+':'+$logstring2)
+ ~~~~~~~~
+CategoryInfo:InvalidData:(:)[Add Content],ParameterBindingValidationException
+FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand
如果使用“&”,则执行ps1,并且创建的变量在该范围内可用。您可以这样更改范围:
$test # default scope
$global:test # global scope
$script:test # script scope
更好的解决方案是使用“.”而不是“&”,因此来自ps1的代码包含在另一个ps1中
所以作用域是相同的,因为它在同一个脚本中。Ehhh。。。创建另一个
.ps1
文件,定义参数参数并调用脚本?不清楚这里的问题是什么。我创建了第二个文件,并在第一个文件中调用了它,但我的参数已被识别。请注意,向我们显示两个脚本+您收到的任何错误。我用这些信息编辑了我的文章。请用&
替换
来解释。这帮了大忙!!