Page 1 of 1

Running .ps1 script remotely

Posted: Thu Jan 25, 2018 11:43 am
by sekou2331
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


Re: Running .ps1 script remotely

Posted: Thu Jan 25, 2018 11:53 am
by jvierra
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.

Re: Running .ps1 script remotely

Posted: Thu Jan 25, 2018 12:20 pm
by sekou2331
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

Re: Running .ps1 script remotely

Posted: Thu Jan 25, 2018 1:59 pm
by sekou2331
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


Re: Running .ps1 script remotely

Posted: Thu Jan 25, 2018 2:15 pm
by jvierra
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.

Re: Running .ps1 script remotely

Posted: Thu Jan 25, 2018 2:42 pm
by sekou2331
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"}"

Re: Running .ps1 script remotely

Posted: Thu Jan 25, 2018 2:50 pm
by mxtrinidad
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.

Re: Running .ps1 script remotely

Posted: Fri Jan 26, 2018 4:19 am
by sekou2331
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.

Re: Running .ps1 script remotely

Posted: Fri Jan 26, 2018 9:16 am
by jvierra
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.