PowerShell代码的奇怪行为 - 除非重新启动会话,否则返回错误的结果

发布于 2025-02-08 21:38:12 字数 564 浏览 0 评论 0原文

我有此代码检查是否存在SSISDB中的项目。在第一次运行中,我确保该项目在那里,并返回正确的值。但是随后我删除项目并再次运行代码,但然后再次返回1。当我重新启动会话时,它开始再次返回正确的答案。问题是什么,我该如何解决?

import-module sqlserver;
$TargetInstanceName = "localhost\default"
$TargetFolderName = "FolderForTesting";
$ProjectName = "ProjectTesting";

$catalog = Get-Item SQLSERVER:\SSIS\$TargetInstanceName\Catalogs\SSISDB\
$folder = $catalog.Folders["$TargetFolderName"];
$project = $folder.Projects["$ProjectName"];

       if($null -eq $project){
            Return 0

        } else {
            Return 1            
        }

I have this code which check for the existence of a project in SSISDB. On the first run I make sure the project is there and it returns the correct value. But then I delete the project and run the code again but it then returns 1 again. When I restart the session then it starts to return the correct answer again. What is the problem and how can I solve it?

import-module sqlserver;
$TargetInstanceName = "localhost\default"
$TargetFolderName = "FolderForTesting";
$ProjectName = "ProjectTesting";

$catalog = Get-Item SQLSERVER:\SSIS\$TargetInstanceName\Catalogs\SSISDB\
$folder = $catalog.Folders["$TargetFolderName"];
$project = $folder.Projects["$ProjectName"];

       if($null -eq $project){
            Return 0

        } else {
            Return 1            
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2025-02-15 21:38:12

结合我的和 theo 对可能的解决方案的有用评论:

import-module sqlserver;
$TargetInstanceName = "localhost\default"
$TargetFolderName = "FolderForTesting";
$ProjectName = "ProjectTesting";

try {
    $catalog = Get-Item SQLSERVER:\SSIS\$TargetInstanceName\Catalogs\SSISDB\  
    $folder = $catalog.Folders[ $TargetFolderName ]
    $project = $folder.Projects[ $ProjectName ]

    if($null -eq $project){
        Return 0
    } else {
        $project.Refresh()  # Causes an exception if project actually doesn't exist
        Return 1            
    }
}
catch {
    return 0
}

这是基于刷新SQL Server PowerShell提供商 ps + sqlps'> ps + sqlps刷新SQL Server对象和您自己的测试。我找不到有关该主题的任何官方信息。

Combining mine and Theo's helpful comments into a possible solution:

import-module sqlserver;
$TargetInstanceName = "localhost\default"
$TargetFolderName = "FolderForTesting";
$ProjectName = "ProjectTesting";

try {
    $catalog = Get-Item SQLSERVER:\SSIS\$TargetInstanceName\Catalogs\SSISDB\  
    $folder = $catalog.Folders[ $TargetFolderName ]
    $project = $folder.Projects[ $ProjectName ]

    if($null -eq $project){
        Return 0
    } else {
        $project.Refresh()  # Causes an exception if project actually doesn't exist
        Return 1            
    }
}
catch {
    return 0
}

This is based on Refreshing the SQL Server PowerShell Provider and PS + SQLPS refreshing the SQL Server object and your own testing. I couldn't find any official information regarding the topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文