Running .ps1 script remotely

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
sekou2331
Posts: 262
Joined: Wed Aug 15, 2012 6:07 am

Running .ps1 script remotely

Post by sekou2331 » Thu Jan 25, 2018 12:43 pm

I am running the below script and I keep getting the same error. I have full permissions to the server. I also can use use Enter-PSSession to access the server. But I cant seem to run the below script. Also I have access to the path that it is writing to. Basically I can run this script on the server itself with no errors but I am trying to run it remotely and it is failing.


Invoke-Command -ComputerName servername -FilePath 'C:\folder\script.ps1' -Credential 'domain\username'



ERROR: Exception calling "Open" with "0" argument(s): "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'."
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
ERROR: + FullyQualifiedErrorId : SqlException
ERROR: + PSComputerName : Servername
ERROR:
ERROR: Exception calling "ExecuteReader" with "0" argument(s): "ExecuteReader requires an open and available Connection. The connection's current state is closed."
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
ERROR: + FullyQualifiedErrorId : InvalidOperationException
ERROR: + PSComputerName : Servername
ERROR:
ERROR: Exception calling "Load" with "1" argument(s): "Value cannot be null.
ERROR: Parameter name: dataReader"
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
ERROR: + FullyQualifiedErrorId : ArgumentNullException
ERROR: + PSComputerName : Servername
ERROR:
ERROR: Access to the path '\\UNCPath\file.csv' is denied.
ERROR: + CategoryInfo : OpenError: (:) [Export-Csv], UnauthorizedAccessException
ERROR: + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
ERROR: + PSComputerName : Servername


User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Running .ps1 script remotely

Post by jvierra » Thu Jan 25, 2018 12:53 pm

ERROR: Access to the path '\\UNCPath\file.csv' is denied.

You cannot remotely access a third server. The CSV file must be on the remote server.

User avatar
sekou2331
Posts: 262
Joined: Wed Aug 15, 2012 6:07 am

Re: Running .ps1 script remotely

Post by sekou2331 » Thu Jan 25, 2018 1:20 pm

Ok I see but why is still trying to access via "NT AUTHORITY\ANONYMOUS LOGON" instead of my user login?



Exception calling "Open" with "0" argument(s): "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
+ PSComputerName : servername

Exception calling "ExecuteReader" with "0" argument(s): "ExecuteReader requires an open and available Connection. The connection's current
state is closed."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
+ PSComputerName : servername

Exception calling "Load" with "1" argument(s): "Value cannot be null.
Parameter name: dataReader"
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
+ PSComputerName : servername

User avatar
sekou2331
Posts: 262
Joined: Wed Aug 15, 2012 6:07 am

Re: Running .ps1 script remotely

Post by sekou2331 » Thu Jan 25, 2018 2:59 pm

Ok I see what the issue is I think. In the .ps1 file I have a function that is calling a DB and it doesn't seem to like the call. I added the login creds as below and it still doesn't like it. Are there limitation to running scripts remotely when call a DB?


Code: Select all

function Get-DBData{
    [CmdletBinding()]
    param(
        [string] $dataSource = 'servername',
		[string]$user = 'Useranme',
		[string]$pwd = 'Password',
        [string] $database = 'DB',
        [string] $connectionString = ('Server={0};uid={1}; pwd={2};Database={3};Integrated Security=False;' -f $dataSource, $user, $pwd, $database)
		
		)
      process{
      
      
    $query = "select * from string query"
    $connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
    $connection.ConnectionString = $connectionString
    #$connection.ConnectionString = "Server=$dataSource;Database=$database;Integrated Security=True;"
    $connection.Open()
    $command = $connection.CreateCommand()
    $command.CommandText = $query

    $result = $command.ExecuteReader()

    $table = new-object -TypeName 'System.Data.DataTable'
    $table.Load($result)
    
    
        $all_needed_servers = $table 


        $all_needed_servers | ForEach-Object { $d_drivepath = $_.something -replace ':', '$' }

        return $all_needed_servers.something 
   }
}

Get-DBData


User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Running .ps1 script remotely

Post by jvierra » Thu Jan 25, 2018 3:15 pm

YOU cannot use explicit credentials on a DB if it is set to "Windows Integrated Authentication". You cannot use Windows authentication to a third server. You cannot access a third server for anything in a remote session.

These things can be done but the remote server and the local client must be enabled and configured for CredSSP which is normally no allowed in a domain for security reasons.

User avatar
sekou2331
Posts: 262
Joined: Wed Aug 15, 2012 6:07 am

Re: Running .ps1 script remotely

Post by sekou2331 » Thu Jan 25, 2018 3:42 pm

Thanks. I think I will just set the code as a schedule Task and run the below when I need it.


"Invoke-Command -ComputerName computername -scriptblock {Start-ScheduledTask -TaskName "task"}"

User avatar
mxtrinidad
Site Admin
Posts: 224
Joined: Sun Mar 03, 2013 12:42 pm

Re: Running .ps1 script remotely

Post by mxtrinidad » Thu Jan 25, 2018 3:50 pm

Maybe the approach need to change.

If the script runs without any issues locally, why do you need to run it remotely?

You don't need to run the script on the server for collecting data. The script includes the connection to the database and can provide the information directly to your desk.

In the other hand, if is a requirement to set it on the server, then you could setup a scheduled task (Task Scheduler or SQL Agent) to execute the script.
No need to use the Invoke-Command as it not necessary.

User avatar
sekou2331
Posts: 262
Joined: Wed Aug 15, 2012 6:07 am

Re: Running .ps1 script remotely

Post by sekou2331 » Fri Jan 26, 2018 5:19 am

It is set in scheduled task. What I am trying to achieve is being able to run it manually anytime needed. So other then it's everyday run I may need to run it during the day. With that being said there are over 100 servers that will be running the script. So I was trying to figure out a way to run it remotely when needed from one server to trigger the script on any of the 100 servers.

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Running .ps1 script remotely

Post by jvierra » Fri Jan 26, 2018 10:16 am

Why does the script have to be run on the remote servers. Just run it locally.

Without a copy of your script there is really no way to advise you.

Locked