Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';powershell是否保留本机类型?_Powershell_Powershell 3.0_Smo - Fatal编程技术网

为什么';powershell是否保留本机类型?

为什么';powershell是否保留本机类型?,powershell,powershell-3.0,smo,Powershell,Powershell 3.0,Smo,请考虑名为NewDB.ps1的powershell脚本中的以下代码: param ($i, $d) Function New-Database { param ($instance, $name) $db = New-Object Microsoft.SqlServer.Management.Smo.Database ($instance, $name) try { $db.Create() } catch [Exception] { $_.Exceptio

请考虑名为
NewDB.ps1
的powershell脚本中的以下代码:

param ($i, $d)

Function New-Database 
{
    param ($instance, $name)
    $db = New-Object Microsoft.SqlServer.Management.Smo.Database ($instance, $name) 
    try { $db.Create() }
    catch [Exception] { $_.Exception | Format-list | Out-String ; exit }
    $db.CreateDate.DateTime
    $db.DatabaseOptions.RecoveryModel
    return $db
}

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$sqlServer = new-Object Microsoft.SqlServer.Management.Smo.Server($i)
[Microsoft.SqlServer.Management.Smo.Database]$myDB = New-Database $sqlServer $d
我得到一个类型转换错误:

PS C:\Users\moomin\Documents> .\NewDB.ps1 "(local)" "testDB"
Cannot convert the "System.Object[]" value of type "System.Object[]" to type
"Microsoft.SqlServer.Management.Smo.Database".
At C:\Users\moomin\Documents\NewDB.ps1:21 char:1
+ [Microsoft.SqlServer.Management.Smo.Database]$myDB = New-Database $sqlServer $d
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException
如何从函数中返回对象作为Microsoft.SqlServer.Management.Smo.Database对象?我需要在功能之外用它做更多的事情

更新 当然,谢谢@mjolinor,这正是我现在想要的。顺便说一句,它还可以在单独的模块中使用
新数据库
(我是如何实际实现它的)


一个函数应该只返回一种类型的对象,并且它输出的任何内容都是返回的一部分,而不仅仅是返回关键字后面的内容,因此您的返回将是这3个对象的数组(这就是为什么它说您的返回类型是object[])


如果您只需要$db,则只返回$db。

函数应该只返回一种类型的对象,并且它输出的任何内容都是返回的一部分,而不仅仅是返回关键字后面的内容,因此您的返回将是这3个对象的数组(这就是为什么它说您的返回类型是object[])


如果您只需要$db,只返回$db。

return关键字实际上只是用于语法。如果在return关键字之前调用任何对象,它们也将返回。因此,本质上,您返回的是一个对象列表,这正是错误告诉您的

让我们以这个为例。此代码将返回一个由$y值组成的数组,但不会返回$x

function return-test {
    $x = 1
    $y = 2

    write-host $x  # Console output only
    $x | out-null  # Goes to null
    $x > $null     # Goes to null
    [void] $x      # Goes to null

    $y             # is returned
    return $y      # is returned, but now the return value is an array.
}
因此,您需要做的是决定使用
$db.CreateDate.DateTime
$db.DatabaseOptions.RecoveryModel
执行什么操作。如果您只想在控制台上显示它们,那么可以像这样使用
write host

function New-Database {
    ...
    write-host $db.CreateDate.DateTime
    write-host $db.DatabaseOptions.RecoveryModel

    # You can use return if you prefer to see it.
    # Either way, you want to return one value.
    #return $db
    $db
}

return关键字实际上只是用于语法。如果在return关键字之前调用任何对象,它们也将返回。因此,本质上,您返回的是一个对象列表,这正是错误告诉您的

让我们以这个为例。此代码将返回一个由$y值组成的数组,但不会返回$x

function return-test {
    $x = 1
    $y = 2

    write-host $x  # Console output only
    $x | out-null  # Goes to null
    $x > $null     # Goes to null
    [void] $x      # Goes to null

    $y             # is returned
    return $y      # is returned, but now the return value is an array.
}
因此,您需要做的是决定使用
$db.CreateDate.DateTime
$db.DatabaseOptions.RecoveryModel
执行什么操作。如果您只想在控制台上显示它们,那么可以像这样使用
write host

function New-Database {
    ...
    write-host $db.CreateDate.DateTime
    write-host $db.DatabaseOptions.RecoveryModel

    # You can use return if you prefer to see it.
    # Either way, you want to return one value.
    #return $db
    $db
}

如果返回了一个对象数组,我可以检查对象数组并使用Smo.Database对象吗?在问题的更新中添加了我的完整解决方案。如果我返回了一个对象数组,我可以检查对象数组并使用Smo.Database对象吗?在问题的更新中添加了我的完整解决方案。