我得到了
Invoke-Command:找不到可以接受参数的位置参数
在[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)] [string] $IPAddress,
[Parameter(Mandatory = $true)] [string] $Username,
[Parameter(Mandatory = $true)] [string] $Password,
[Parameter(Mandatory = $true)] [string[]][AllowNull()][AllowEmptyCollection()] $DatabaseList = @(),
[Parameter(Mandatory = $true)] [string] $source_folder
)
$Creds = Create-Credentials -Username $Username -Password $Password
function Start-Prerequisites {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)] [string] $source_folder,
[Parameter(Mandatory = $true)] [string[]][AllowNull()][AllowEmptyCollection()] $DatabaseList = @()
)
for ($i=0; $i -le $DatabaseList.Count -1 ; $i++) {
$j = $i+1
Write-Host "Database $j is " $DatabaseList[$i]
$DatabaseName = $DatabaseList[$i]
$BackUpQuery =
"USE master
GO
--Step-1 : Remove from Availability Group
BEGIN
TRY
ALTER AVAILABILITY GROUP AETEST01 REMOVE DATABASE $DatabaseName
IF @@ERROR <>0
BEGIN
PRINT 'Error occured while removing Databases from AVAILABILITY GROUP'
RETURN
END
PRINT 'Databases are removing from AVAILABILITY GROUP Successfull!!'
--Step-2 : Take Backup of the database
Backup Database $DatabaseName To Disk = 'G:\MediumProfile\$DatabaseName.bak' With Format;
IF @@ERROR <>0
BEGIN
PRINT 'Error occured while taking backups'
RETURN
END
PRINT 'Backups taken Successfull!!'
--Step-3 : Add the database back to Availability Group
ALTER AVAILABILITY GROUP AETEST01 ADD DATABASE $DatabaseName
END TRY
BEGIN CATCH
PRINT 'Errors occurred!'
END CATCH"
}
Write-Host "Backup Script executing"
$SQLServer = Invoke-Command -ComputerName $IPAddress -Credential $Creds -ScriptBlock {
if (Test-Path $args[0]) {
Write-Host $args[0]" folder is exist"
$BackFolderContentsValidator = Get-ChildItem -Path $args[0] | Measure-Object
if (-not ($BackFolderContentsValidator.Count -eq 0)) {
Write-Host "The Folder is --> "$args[0]
$ServerInstance = "jmallick\SQLSERVER2012"
$Query = "$args[1]"
$BackupDatabase = Invoke-Sqlcmd -ServerInstance $ServerInstance -Database master -Query $Query
if ($?) {
Write-Host "Back process complete"
} else {
Write-Warning "Something wrong in the execution"
}
} else {
Write-Warning "Source folder is exist, but there are no back up files found inside it !!!"
}
} else {
Write-Warning "Source folder is exist, but there are no back up files found inside!!!"
}
} -ArgumentList $source_folder $BackUpQuery
Start-Prerequisites -source_folder $source_folder -DatabaseList $DatabaseList
内运行SQL命令时。
尝试将值存储为数组,对我没有帮助。
Invoke-Command
在尝试验证数据库之前,我要进行一些备份,例如要保留备份文件的文件夹是否存在,是否存在其他文件。
因此在Invoke-Sqlcmd
内,我在上述条件下进行了验证,之后,我试图通过{{1}}进行备份,该查询需要一个查询字符串,并且将此查询作为参数传递给脚本块