# Dependancy for invoke-sqlcmd

**URL:** <https://discourse.chef.io/t/dependancy-for-invoke-sqlcmd/19172>\
**Category:** Habitat Help\
**Created:** [November 15, 2018, 8:57pm UTC](https://discourse.chef.io/t/dependancy-for-invoke-sqlcmd/19172 "2018-11-15T20:57:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![devopslifter](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/devopslifter/32/2737_2.png) [@devopslifter](https://discourse.chef.io/u/devopslifter)\
**Post date:** [November 15, 2018, 8:57pm UTC](https://discourse.chef.io/t/dependancy-for-invoke-sqlcmd/19172/1 "2018-11-15T20:57:19Z")

</div>

Hi All, trying to figure out what dependancy I need to use in habitat to use invoke-sqlcmd

---

<div class="post-metadata">

**Author:** ![Matt\_Wrock](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/matt_wrock/32/3394_2.png) [@Matt\_Wrock](https://discourse.chef.io/u/Matt_Wrock)\
**Post date:** [November 15, 2018, 10:20pm UTC](https://discourse.chef.io/t/dependancy-for-invoke-sqlcmd/19172/2 "2018-11-15T22:20:20Z")

</div>

Your best option here I think s to use the `sqlserver` powershell module. However there are some unobvious nuances when it comes to using this module in a Habitat hook. The key issue being that Habitat hooks run inside of powershell core but the `Invoke-SqlCmd` cmdlet uses functionality only available in Windows Powershell. So the best course of action here is to install and use the module inside of a Windows Powershell context. How do you do that in a Habitat hook? Use `Invoke-Command` from powershell core to enter into a Windows Powershell runspace:

```auto
Invoke-Command -ComputerName localhost -EnableNetworkAccess {
    $ProgressPreference="SilentlyContinue"
    Write-Host "Checking for nuget package provider..."
    if(!(Get-PackageProvider -Name nuget -ErrorAction SilentlyContinue -ListAvailable)) {
        Write-Host "Installing Nuget provider..."
        Install-PackageProvider -Name NuGet -Force | Out-Null
    }
    Write-Host "Checking for SqlServer PS module..."
    if(!(Get-Module SqlServer -ListAvailable)) {
        Write-Host "Installing SqlServer PS Module..."
        Install-Module SqlServer -Force -AllowClobber | Out-Null
    }

    {{#if bind.database}}
    $server = "{{bind.database.first.sys.ip}}\\"
    $server += "{{bind.database.first.cfg.instance}},"
    $server += "{{bind.database.first.cfg.port}}"
    
    Invoke-Sqlcmd -ServerInstance $server -Database Master -Username sa -Password Pass@word1 -InputFile {{pkg.svc_var_path}}\sql_scripts\a_bunch_of_sql.sql
    {{/if}}
}

```

We are effectively “remoting” to `localhost` in order to enter a Windows Powershell process.
