Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a credential


This recipe goes through the code needed for creating a SQL Server credential.

Getting ready

In this recipe, we will create a credential for a domain account that has access to certain files and folders in our system, QUERYWORKS\filemanager. Here's the equivalent T-SQL of what we are trying to accomplish:

CREATE CREDENTIAL [filemanagercred]
WITH IDENTITY = N'QUERYWORKS\filemanager',
SECRET = N'YourSuperStrongPassword'

You can substitute this with another known Windows account you have in your environment.

How to do it...

These are the steps to create a credential:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module and create a new SMO Server Object:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #replace this with your instance name
    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
  3. Add the following script and run:

    $identity = "QUERYWORKS\filemanager"
    $credentialName...