Friday, May 27, 2016

SQLPS NOT LOADING (execution of scripts is disabled)

To change your execution policy, type:

        Set-ExecutionPolicy -ExecutionPolicy <PolicyName>

    For example:

        Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Use case scenario:

As a DBA you need to routinely check databases in your SQLServer, name, size, and spaceavailable, indexspaceusage. Enter the following in powershell
 




 none
SQLPS used to work before sql update

    Question

  • I have a few .ps1 files being called from sql jobs that used to work and now i'm getting an error when the SQLPS module tries to load. The error specifically says: "...job step recieved an error at line 1 in Powershell script. The corresponding line is 'import module SQLPS -DisableNameChecking."
    The job step is running like this -
    Start-Process PowerShell -ArgumentList "& 'C:\NewInstanceScripts\SPLA_Users.ps1'"
    The contents of the .ps1 file are as follows.  This and others like it work fine in ps direct. just not when called from a sql job.

      
    #Pull all group members into csv file
    Get-ADGroupMember “CRDS Users” | where {$_.objectclass-eq “user”} | Select SamAccountName | Export-csv -path C:\SPLA_Users.csv -NoTypeInformation

    #Insert contents of csv into SQL table
    $sqlsvr = 'SQL03'
    $database = 'db_SPLA'
    $table = 'tbl_CRDSUsers'


    #Create SQL Connection
    Write-Verbose "Creating SQL Connection"

    $conn = New-Object System.Data.SqlClient.SqlConnection("Server=$SQLSvr;Database=$Database;Integrated Security=True")
    $conn.Open()
    $cmd = $conn.CreateCommand()

    #Create a delete statement if want to replace contents each time
    #$cmd2 = $conn.CreateCommand()
    #$cmd2.commandtext = "Delete From $table"
    #Write-Host -Fore Green "Clean Table Contents"
    #$cmd2.ExecuteNonQuery()


    Import-Csv C:\SPLA_Users.csv | % {
      #$_.Displayname
      #$_.Version

      #Create query string
      #Must matching the table layout (SoftwareName, Version)
      Write-Host -Fore Green "Inserting $_.SamAccountName into Table"
      $cmd.CommandText = "INSERT INTO $table (SamAccountName,UploadDate) VALUES ('$($_.SamAccountName)', '$(get-date -f MM-dd-yyyy)')"

      #Execute Query
      $cmd.ExecuteNonQuery() | Out-Null
      }

    The only thing i changed was the version of SQL Server 2012
    • Edited by Dave_Odom Tuesday, June 03, 2014 1:52 PM
    Tuesday, June 03, 2014 1:47 PM
    Avatar of Dave_Odom
    Dave_Odom
    5
    Points
    Dave_Odom
    Partner Joined Feb 2008
    4
    (Partner)
    5 Points

All replies

  • Hi Dave,
    Do you mean errors occured when calling a powershell script in SQL agent job?
    I'm also a little confused about the error, because I can not find the cmdlet "import-module SQLPS -DisableNameChecking" in the .ps1 file you posted above.
    Would you please post the hard code and its error information in detail, which will help us to troubleshoot?
    I recommend you can run the .ps1 file in powershell before calling in SQL agent job, and check if the script has any error.
    In addition, the script would get error, if there has UNC file path after running "import-Module 'sqlps'", for more detailed information, please check this thread:
    Powershell from a  SQL Agent Job (SQL Server 2012)
    If you have any feedback on our support, please click here.

    Best Regards,
    Anna
    TechNet Community Support
    Wednesday, June 04, 2014 2:11 PM
    Avatar of AnnaWY
    AnnaWY
    18,205
    Points
    Top 0.5
    AnnaWY
    Joined Aug 2013
    3 7 13
    18,205 Points
    Moderator
  • Yes, i have a sql agent job and that is what fails.  the import-module is implicit in that ps tries to import the cmdlet without my intervention.  I've tried adding to the .ps1 and get the same result.
    I have run in ps direct and it works perfectly, no errors.
    The next part of the error if i look at the job history details is "
    Message
    Executed as user: CLIENT\svc-sql03. A job step received an error at line 1 in a PowerShell script. The corresponding line is 'import-module SQLPS  -DisableNameChecking'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'File C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.  File C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.  '.  Process Exit Code -1.  The step failed."

    I've run set-executionpolicy remotesigned on the SQL server on both the 64(C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe) and 32(C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe) bit powershell.exe (run as admin)
    Wednesday, June 04, 2014 7:07 PM
    Avatar of Dave_Odom
    Dave_Odom
    5
    Points
    Dave_Odom
    Partner Joined Feb 2008
    4
    (Partner)
    5 Points
  • Try this ... I think this will be the issue ..Check the registry for :
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps110\ExecutionPolicy=Unrestricted
    for me the registry value wasn't there and i created the key and string value Microsoft.SqlServer.Management.PowerShell.sqlps110 and ExecutionPolicy=Unrestricted and it worked .
    You may wanna check this link  as well: http://sqlandy.com/2014/01/uninstalling-ssms-on-server-breaks-sql-powershell/
    Monday, October 13, 2014 10:09 AM
    Avatar of SleepingSpid
    210 Points

Monday, May 23, 2016

Ports Used By SQL Server

The following tables can help you identify the ports being used by SQL Server.

Ports Used By the Database Engine

The following table lists the ports that are frequently used by the Database Engine.
ScenarioPortComments
SQL Server default instance running over TCPTCP port 1433This is the most common port allowed through the firewall. It applies to routine connections to the default installation of the Database Engine, or a named instance that is the only instance running on the computer. (Named instances have special considerations. See Dynamic Ports later in this topic.)
SQL Server named instances in the default configurationThe TCP port is a dynamic port determined at the time the Database Engine starts.See the discussion below in the section Dynamic Ports. UDP port 1434 might be required for the SQL Server Browser Service when you are using named instances.
SQL Server named instances when they are configured to use a fixed portThe port number configured by the administrator.See the discussion below in the section Dynamic Ports.
Dedicated Admin ConnectionTCP port 1434 for the default instance. Other ports are used for named instances. Check the error log for the port number.By default, remote connections to the Dedicated Administrator Connection (DAC) are not enabled. To enable remote DAC, use the Surface Area Configuration facet. For more information, see Surface Area Configuration.
SQL Server Browser serviceUDP port 1434The SQL Server Browser service listens for incoming connections to a named instance and provides the client the TCP port number that corresponds to that named instance. Normally the SQL Server Browser service is started whenever named instances of the Database Engine are used. The SQL Server Browser service does not have to be started if the client is configured to connect to the specific port of the named instance.
SQL Server instance running over an HTTP endpoint.Can be specified when an HTTP endpoint is created. The default is TCP port 80 for CLEAR_PORT traffic and 443 for SSL_PORT traffic.Used for an HTTP connection through a URL.
SQL Server default instance running over an HTTPS endpoint.TCP port 443Used for an HTTPS connection through a URL. HTTPS is an HTTP connection that uses secure sockets layer (SSL).
Service BrokerTCP port 4022. To verify the port used, execute the following query:

 SELECT name, protocol_desc, port, state_desc

 FROM sys.tcp_endpoints

 WHERE type_desc = 'SERVICE_BROKER'
There is no default port for SQL ServerService Broker, but this is the conventional configuration used in Books Online examples.
Database MirroringAdministrator chosen port. To determine the port, execute the following query:

 SELECT name, protocol_desc, port, state_desc FROM sys.tcp_endpoints

 WHERE type_desc = 'DATABASE_MIRRORING'
There is no default port for database mirroring however Books Online examples use TCP port 7022. It is very important to avoid interrupting an in-use mirroring endpoint, especially in high-safety mode with automatic failover. Your firewall configuration must avoid breaking quorum. For more information, see Specify a Server Network Address (Database Mirroring).
ReplicationReplication connections to SQL Server use the typical regular Database Engine ports (TCP port 1433 for the default instance, etc.)

Web synchronization and FTP/UNC access for replication snapshot require additional ports to be opened on the firewall. To transfer initial data and schema from one location to another, replication can use FTP (TCP port 21), or sync over HTTP (TCP port 80) or File Sharing. File sharing uses UDP port 137 and 138, and TCP port 139 if it using NetBIOS. File Sharing uses TCP port 445.
For sync over HTTP, replication uses the IIS endpoint (ports for which are configurable but is port 80 by default), but the IIS process connects to the backend SQL Server through the standard ports (1433 for the default instance.

During Web synchronization using FTP, the FTP transfer is between IIS and the SQL Server publisher, not between subscriber and IIS.
Transact-SQL debuggerTCP port 135

See Special Considerations for Port 135

The IPsec exception might also be required.
If using Visual Studio, on the Visual Studio host computer, you must also add Devenv.exe to the Exceptions list and open TCP port 135.

If using Management Studio, on the Management Studio host computer, you must also add ssms.exe to the Exceptions list and open TCP port 135. For more information, see Configure firewall rules before running the TSQL Debugger.
For step by step instructions to configure the Windows Firewall for the Database Engine, see Configure a Windows Firewall for Database Engine Access.

Dynamic Ports

By default, named instances (including SQL Server Express) use dynamic ports. That means that every time that the Database Engine starts, it identifies an available port and uses that port number. If the named instance is the only instance of the Database Engine installed, it will probably use TCP port 1433. If other instances of the Database Engine are installed, it will probably use a different TCP port. Because the port selected might change every time that the Database Engine is started, it is difficult to configure the firewall to enable access to the correct port number. Therefore, if a firewall is used, we recommend reconfiguring the Database Engine to use the same port number every time. This is called a fixed port or a static port. For more information, see Configure a Server to Listen on a Specific TCP Port (SQL Server Configuration Manager).
An alternative to configuring a named instance to listen on a fixed port is to create an exception in the firewall for a SQL Server program such as sqlservr.exe (for the Database Engine). This can be convenient, but the port number will not appear in the Local Port column of the Inbound Rules page when you are using the Windows Firewall with Advanced Security MMC snap-in. This can make it more difficult to audit which ports are open. Another consideration is that a service pack or cumulative update can change the path to the SQL Server executable which will invalidate the firewall rule.
System_CAPS_ICON_note.jpg Note

The following procedure uses the Windows Firewall item in Control Panel. The Windows Firewall with Advanced Security MMC snap-in can configure a more complex rule. This includes configuring a service exception which can be useful for providing defense in depth. See Using the Windows Firewall with Advanced Security Snap-in below.
To add a program exception to the firewall using the Windows Firewall item in Control Panel.
  1. On the Exceptions tab of the Windows Firewall item in Control Panel, click Add a program.
  2. Browse to the location of the instance of SQL Server that you want to allow through the firewall, for example C:\Program Files\Microsoft SQL Server\MSSQL13.<instance_name>\MSSQL\Binn, select sqlservr.exe, and then click Open.
  3. Click OK.
For more information about endpoints, see Configure the Database Engine to Listen on Multiple TCP Ports and Endpoints Catalog Views (Transact-SQL).

Ports Used By Analysis Services

The following table lists the ports that are frequently used by Analysis Services.
FeaturePortComments
Analysis ServicesTCP port 2383 for the default instanceThe standard port for the default instance of Analysis Services.
SQL Server Browser serviceTCP port 2382 only needed for an Analysis Services named instanceClient connection requests for a named instance of Analysis Services that do not specify a port number are directed to port 2382, the port on which SQL Server Browser listens. SQL Server Browser then redirects the request to the port that the named instance uses.
Analysis Services configured for use through IIS/HTTP

(The PivotTable® Service uses HTTP or HTTPS)
TCP port 80Used for an HTTP connection through a URL.
Analysis Services configured for use through IIS/HTTPS

(The PivotTable® Service uses HTTP or HTTPS)
TCP port 443Used for an HTTPS connection through a URL. HTTPS is an HTTP connection that uses secure sockets layer (SSL).
If users access Analysis Services through IIS and the Internet, you must open the port on which IIS is listening and specify that port in the client connection string. In this case, no ports have to be open for direct access to Analysis Services. The default port 2389, and port 2382, should be restricted together with all other ports that are not required.
For step by step instructions to configure the Windows Firewall for Analysis Services, see Configure the Windows Firewall to Allow Analysis Services Access.

Ports Used By Reporting Services

The following table lists the ports that are frequently used by Reporting Services.
FeaturePortComments
Reporting Services Web ServicesTCP port 80Used for an HTTP connection to Reporting Services through a URL. We recommend that you do not use the preconfigured rule World Wide Web Services (HTTP). For more information, see the Interaction with Other Firewall Rules section below.
Reporting Services configured for use through HTTPSTCP port 443Used for an HTTPS connection through a URL. HTTPS is an HTTP connection that uses secure sockets layer (SSL). We recommend that you do not use the preconfigured rule Secure World Wide Web Services (HTTPS). For more information, see the Interaction with Other Firewall Rules section below.
When Reporting Services connects to an instance of the Database Engine or Analysis Services, you must also open the appropriate ports for those services. For step-by-step instructions to configure the Windows Firewall for Reporting Services, Configure a Firewall for Report Server Access.

Ports Used By Integration Services

The following table lists the ports that are used by the Integration Services service.
FeaturePortComments
Microsoft remote procedure calls (MS RPC)

Used by the Integration Services runtime.
TCP port 135

See Special Considerations for Port 135
The Integration Services service uses DCOM on port 135. The Service Control Manager uses port 135 to perform tasks such as starting and stopping the Integration Services service and transmitting control requests to the running service. The port number cannot be changed.

This port is only required to be open if you are connecting to a remote instance of the Integration Services service from Management Studio or a custom application.
For step-by-step instructions to configure the Windows Firewall for Integration Services, see Configure a Windows Firewall for Access to the SSIS Service.

Additional Ports and Services

The following table lists ports and services that SQL Server might depend on.
ScenarioPortComments
Windows Management Instrumentation

For more information about WMI, see WMI Provider for Configuration Management Concepts
WMI runs as part of a shared service host with ports assigned through DCOM. WMI might be using TCP port 135.

See Special Considerations for Port 135
SQL Server Configuration Manager uses WMI to list and manage services. We recommend that you use the preconfigured rule group Windows Management Instrumentation (WMI). For more information, see the Interaction with Other Firewall Rules section below.
Microsoft Distributed Transaction Coordinator (MS DTC)TCP port 135

See Special Considerations for Port 135
If your application uses distributed transactions, you might have to configure the firewall to allow Microsoft Distributed Transaction Coordinator (MS DTC) traffic to flow between separate MS DTC instances, and between the MS DTC and resource managers such as SQL Server. We recommend that you use the preconfigured Distributed Transaction Coordinator rule group.

When a single shared MS DTC is configured for the entire cluster in a separate resource group you should add sqlservr.exe as an exception to the firewall.
The browse button in Management Studio uses UDP to connect to the SQL Server Browser Service. For more information, see SQL Server Browser Service (Database Engine and SSAS).UDP port 1434UDP is a connectionless protocol.

The firewall has a setting, which is named UnicastResponsesToMulticastBroadcastDisabled Property of the INetFwProfile Interface which controls the behavior of the firewall with respect to unicast responses to a broadcast (or multicast) UDP request. It has two behaviors:

If the setting is TRUE, no unicast responses to a broadcast are permitted at all. Enumerating services will fail.

If the setting is FALSE (default), unicast responses are permitted for 3 seconds. The length of time is not configurable. in a congested or high-latency network, or for heavily loaded servers, tries to enumerate instances of SQL Server might return a partial list, which might mislead users.
IPsec trafficUDP port 500 and UDP port 4500If the domain policy requires network communications to be done through IPsec, you must also add UDP port 4500 and UDP port 500 to the exception list. IPsec is an option using the New Inbound Rule Wizard in the Windows Firewall snap-in. For more information, see Using the Windows Firewall with Advanced Security Snap-in below.
Using Windows Authentication with Trusted DomainsFirewalls must be configured to allow authentication requests.For more information, see How to configure a firewall for domains and trusts.
SQL Server and Windows ClusteringClustering requires additional ports that are not directly related to SQL Server.For more information, see Enable a network for cluster use.
URL namespaces reserved in the HTTP Server API (HTTP.SYS)Probably TCP port 80, but can be configured to other ports. For general information, see Configuring HTTP and HTTPS.For SQL Server specific information about reserving an HTTP.SYS endpoint using HttpCfg.exe, see About URL Reservations and Registration (SSRS Configuration Manager).
When you use RPC with TCP/IP or with UDP/IP as the transport, inbound ports are frequently dynamically assigned to system services as required; TCP/IP and UDP/IP ports that are larger than port 1024 are used. These are frequently informally referred to as "random RPC ports." In these cases, RPC clients rely on the RPC endpoint mapper to tell them which dynamic ports were assigned to the server. For some RPC-based services, you can configure a specific port instead of letting RPC assign one dynamically. You can also restrict the range of ports that RPC dynamically assigns to a small range, regardless of the service. Because port 135 is used for many services it is frequently attacked by malicious users. When opening port 135, consider restricting the scope of the firewall rule.
For more information about port 135, see the following references:
The Windows Firewall uses rules and rule groups to establish its configuration. Each rule or rule group is generally associated with a particular program or service, and that program or service might modify or delete that rule without your knowledge. For example, the rule groups World Wide Web Services (HTTP) and World Wide Web Services (HTTPS) are associated with IIS. Enabling those rules will open ports 80 and 443, and SQL Server features that depend on ports 80 and 443 will function if those rules are enabled. However, administrators configuring IIS might modify or disable those rules. Therefore, if you are using port 80 or port 443 for SQL Server, you should create your own rule or rule group that maintains your desired port configuration independently of the other IIS rules.
The Windows Firewall with Advanced Security MMC snap-in allows any traffic that matches any applicable allow rule. So if there are two rules that both apply to port 80 (with different parameters), traffic that matches either rule will be permitted. So if one rule allows traffic over port 80 from local subnet and one rule allows traffic from any address, the net effect is that all traffic to port 80 is permitted regardless of the source. To effectively manage access to SQL Server, administrators should periodically review all firewall rules enabled on the server.
Firewall profiles are discussed in Windows Firewall with Advanced Security Getting Started Guide in the section Network location-aware host firewall. To summarize, the operating systems identify and remember each of the networks to which they connect with regard to connectivity, connections, and category.
There are three network location types in Windows Firewall with Advanced Security:
  • Domain. Windows can authenticate access to the domain controller for the domain to which the computer is joined.
  • Public. Other than domain networks, all networks are initially categorized as public. Networks that represent direct connections to the Internet or are in public locations, such as airports and coffee shops should be left public.
  • Private. A network identified by a user or application as private. Only trusted networks should be identified as private networks. Users will likely want to identify home or small business networks as private.
The administrator can create a profile for each network location type, with each profile containing different firewall policies. Only one profile is applied at any time. Profile order is applied as follows:
  1. If all interfaces are authenticated to the domain controller for the domain of which the computer is a member, the domain profile is applied.
  2. If all interfaces are either authenticated to the domain controller or are connected to networks that are classified as private network locations, the private profile is applied.
  3. Otherwise, the public profile is applied.
Use the Windows Firewall with Advanced Security MMC snap-in to view and configure all firewall profiles. The Windows Firewall item in Control Panel only configures the current profile.
Exceptions that you add to the firewall can restrict the opening of the port to incoming connections from specific computers or the local subnet. This restriction of the scope of the port opening can reduce how much your computer is exposed to malicious users, and is recommended.
System_CAPS_ICON_note.jpg Note

Using the Windows Firewall item in Control Panel only configures the current firewall profile.

To change the scope of a firewall exception using the Windows Firewall item in Control Panel

  1. In the Windows Firewall item in Control Panel, select a program or port on the Exceptions tab, and then click Properties or Edit.
  2. In the Edit a Program or Edit a Port dialog box, click Change Scope.
  3. Choose one of the following options:
    • Any computer (including those on the Internet)
      Not recommended. This will allow any computer that can address your computer to connect to the specified program or port. This setting might be necessary to allow information to be presented to anonymous users on the internet, but increases your exposure to malicious users. Your exposure can be further increased if you enable this setting and also allow Network Address Translation (NAT) traversal, such as the Allow edge traversal option.
    • My network (subnet) only
      This is a more secure setting than Any computer. Only computers on the local subnet of your network can connect to the program or port.
    • Custom list:
    Only computers that have the IP addresses you list can connect. This can be a more secure setting than My network (subnet) only, however, client computers using DHCP can occasionally change their IP address. Then the intended computer will not be able to connect. Another computer, which you had not intended to authorize, might accept the listed IP address and then be able to connect. The Custom list option might be appropriate for listing other servers which are configured to use a fixed IP address; however, IP addresses might be spoofed by an intruder. Restricting firewall rules are only as strong as your network infrastructure.
Additional advanced firewall settings can be configured by using the Windows Firewall with Advanced Security MMC snap-in. The snap-in includes a rule wizard and exposes additional settings that are not available in the Windows Firewall item in Control Panel. These settings include the following:
  • Encryption settings
  • Services restrictions
  • Restricting connections for computers by name
  • Restricting connections to specific users or profiles
  • Edge traversal allowing traffic to bypass Network Address Translation (NAT) routers
  • Configuring outbound rules
  • Configuring security rules
  • Requiring IPsec for incoming connections

To create a new firewall rule using the New Rule wizard

  1. On the Start menu, click Run, type WF.msc, and then click OK.
  2. In the Windows Firewall with Advanced Security, in the left pane, right-click Inbound Rules, and then click New Rule.
  3. Complete the New Inbound Rule Wizard using the settings that you want.
The following tools and techniques can be useful in troubleshooting firewall issues:
  • The effective port status is the union of all rules related to the port. When trying to block access through a port, it can be helpful to review all the rules which cite the port number. To do this, use the Windows Firewall with Advanced Security MMC snap-in and sort the inbound and outbound rules by port number.
  • Review the ports that are active on the computer on which SQL Server is running. This review process includes verifying which TCP/IP ports are listening and also verifying the status of the ports.
    To verify which ports are listening, use the netstat command-line utility. In addition to displaying active TCP connections, the netstat utility also displays a variety of IP statistics and information.

    To list which TCP/IP ports are listening

    1. Open the Command Prompt window.
    2. At the command prompt, type netstat -n -a.
      The -n switch instructs netstat to numerically display the address and port number of active TCP connections. The -a switch instructs netstat to display the TCP and UDP ports on which the computer is listening.
  • The PortQry utility can be used to report the status of TCP/IP ports as listening, not listening, or filtered. (With a filtered status, the port might or might not be listening; this status indicates that the utility did not receive a response from the port.) The PortQry utility is available for download from the Microsoft Download Center.
Service overview and network port requirements for the Windows Server system
How to: Configure Firewall Settings (Azure SQL Database)