Running .ps1 script remotely

Ask your 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.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Running .ps1 script remotely

Post 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

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running .ps1 script remotely

Post 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.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Running .ps1 script remotely

Post 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
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Running .ps1 script remotely

Post 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

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running .ps1 script remotely

Post 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.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Running .ps1 script remotely

Post 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"}"
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Running .ps1 script remotely

Post 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.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Running .ps1 script remotely

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Running .ps1 script remotely

Post 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.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked