Udostępnij za pośrednictwem


powershell and sql snap-in...

· FINDING SNAP-INS

To list the Windows PowerShell snap-ins on your system, type: 

get-pssnapin

 

To find the snap-in for each Windows PowerShell provider, type:  

get-psprovider | format-list name, pssnapin

 

To list the cmdlets in a Windows PowerShell snap-in, type: 

get-command -pssnapin <snapin-name>

 

· INSTALLING A SNAP-IN

The built-in snap-ins are registered in the system and added to the default console when you start Windows PowerShell. However, to use snap-ins that you create or obtain from others, you need to register them and add the to your console.

 

· REGISTERING A SNAP-IN

A Windows PowerShell snap-in is a program written in a .NET language and compiled into a DLL file. To use the providers and cmdlets in a snap-in, you must first register the snap-in (add it to the registry).

Most snap-ins come with an installation program (an EXE or MSI file) that registers the DLL for you. However, if you receive a snap-in as a DLL file, you can register it on your system. For instructions, see the "Register the Snap-in" topic in the Windows PowerShell SDK.

To find all registered snap-ins on your system, or to verify that a snap-in is registered, type:

get-pssnapin -registered

 

· ADD THE SNAP-IN TO THE CONSOLE

To add a registered snap-in to the current console, use the Add-PsSnapin cmdlet. For example, to add the SQL snap-in to the console, type:

 

PS C:\> get-pssnapin -registered

Name : SqlServerCmdletSnapin

PSVersion : 1.0

Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.

Name : SqlServerProviderSnapin

PSVersion : 1.0

Description : SQL Server Provider

PS C:\> cd sql:

Set-Location : Cannot find drive. A drive with name 'sql' does not exist.

At line:1 char:3

+ cd <<<< sql:

PS C:\> add-pssnapin SqlServerProviderSnapin

PS C:\> add-pssnapin SqlServerCmdletSnapin

PS C:\> get-pssnapin

 

Name : SqlServerProviderSnapin

PSVersion : 1.0

Description : SQL Server Provider

Name : SqlServerCmdletSnapin

PSVersion : 1.0

Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.

n Now I can switch to the SQL drive.

PS C:\> cd sql:

PS SQL:\>

As soon as the command completes, the providers and cmdlets in the snap-in are available in the current console. However, the current console is not saved automatically.

 

· SAVING THE CONSOLE CONFIGURATION

To use the snap-in in future Windows PowerShell sessions, you must save the configuration of the current console, which includes the snap-in.

To save the current console configuration to a console file (.psc1), use Export-Console. For example, to save the current console to the NewConsole.psc1 file in the current directory, type:

export-console NewConsole

 

· OPENING A CONSOLE WITH THE SNAP-IN

To open a console with the console configuration that includes the snapin, when starting Windows PowerShell, use the PsConsoleFile parameter to specify the console file that you exported. For example, the following command starts Windows PowerShell with the NewConsole.psc1 console file.

powershell.exe -psconsolefile NewConsole.psc1

 

The providers and cmdlets in the snapin are now available for use.

 

· REMOVING A SNAP-IN

To remove a Windows PowerShell snap-in from the current console, use the Remove-PsSnapin cmdlet. For example, to remove the Windows PowerShell SQL snap-in from the current console, type:

remove-pssnapin sql

 

This cmdlet removes the snap-in from the console. The snap-in is still loaded, but the providers and cmdlets that it supports are no longer available.