gpt4 book ai didi

powershell - 未加载 SQLASCmdlets,因为在任何模块目录中都找不到有效的模块文件

转载 作者:行者123 更新时间:2023-12-02 23:54:41 24 4
gpt4 key购买 nike

我有一个由于缺少 SQLASCmdlets 模块而失败的脚本。

The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.



为了运行它,我知道我需要 SQLPS,我可以使用它找到

Get-Module -ListAvaiable


ModuleType Version    Name
---------- ------- ----
Manifest 1.0 SQLPS

我已经安装
- SQL 服务器 2016
- SQL 管理工作室 2017

错误:
VERBOSE: ******************************
VERBOSE: The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.
VERBOSE: Errors: 1
VERBOSE: ******************************
VERBOSE: System.IO.FileNotFoundException: The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.
VERBOSE: ScriptHalted The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.

当我再次重新运行我的脚本时,它可以工作。我如何使它第一次工作?

我使用的代码是:
Import-Module "SQLPS" -DisableNameChecking


$sqlsvr = New-Object -TypeName Microsoft.SQLServer.Management.Smo.Server("WIN-C0BP65U3D4G")
$restore = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Restore
$devicetype = [Microsoft.SqlServer.Management.Smo.DeviceType]::File

$files = gci C:\psbackups

$BackupFiles = @()

foreach ($file in $files){
$restoredevice = New-Object -TypeName Microsoft.SQLServer.Management.Smo.BackupDeviceItem($file.FullName,$devicetype)

$restore.Devices.add($restoredevice)
$errcnt=0
try{
$restore.ReadMediaHeader($sqlsvr)
}
catch [System.Exception]{
Write-Output "$file is not a sql backup file `n"
$errcnt =1
}
finally {
if ($errcnt -ne 1){
Write-Output "$file is a sql backup file `n"
$BackupFiles += $file
}
$errcnt = 0
}
$restore.Devices.remove($restoredevice)
Remove-Variable restoredevice
}

基于这篇文章
PowerShell to restore database

最佳答案

经过2小时的研究,我找到了解决方案。

  • 安装SqlServer包

  • https://www.powershellgallery.com/packages/SqlServer/21.0.17199

    2.我已更新我的脚本以选择特定版本以防安装
    # import modules 
    if ((Get-Module -ListAvailable | where-object {($_.Name -eq 'SqlServer') -and ($_.Version.Major -gt 20) } |Measure).Count -eq 1){
    # implementation of new sql modules migated into new location
    Import-Module SqlServer -DisableNameChecking -Verbose
    }
    else{
    # fallback for SQLPS
    Import-Module SQLPS -DisableNameChecking -Verbose
    }
  • 确保我有你的 PowerShell 的新 session 。
  • 关于powershell - 未加载 SQLASCmdlets,因为在任何模块目录中都找不到有效的模块文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47890288/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com