How do I use INCLUDE files or similar in string variables?

This topic contains 6 replies, has 2 voices, and was last updated by Profile photo of GS GS 2 years, 1 month ago.

  • Author
    Posts
  • #19957
    Profile photo of GS
    GS
    Participant

    Hello,

    I have a string variable which is assigned to a function similar to below. I'd like to reuse code for variable $scriptToExecute in conditional statements below, how this can be accomlished (similar to INCLUDE directive in ASP files)

    process{
    
    
    	$func = {function DoCleanup
    				{
    					param([string]$computerName)
    					if ($computerName -match ".*SERVER1.*")
    					{
    						$scriptToExecute = {
    						del c:\temp\a.txt #< -- Want this stuff to be reused
    						}
    						}
    					elseif ($computerName -match ".*SERVER2.*")
    					{
    					$scriptToExecute = {
    					del c:\temp\a.txt		
    					}
    					}
    				}
    			Invoke-Command -ComputerName $computerName -ScriptBlock $scriptToExecute
    		}
    	Start-Job -ScriptBlock {DoCleanup $args[0]}
    	}
  • #19958
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The goal is to read and execute arbitrary commands that are in mini-scripts? And you want them to run within the scope of the caller? That's dot-sourcing. Although that'd *run* the script, not load it into a variable. I'm probably not understanding why you want it in a variable.

    Maybe let's break this down.

    The goal is to test the computer name, and based on that, run a script on that computer? Just use the -File parameter of Invoke-Command. That'll read the file locally, transmit its contents to the specified computer, and have the computer run the script.

    If ($computername -match ".*SERVER1.*") {
      Invoke-Command -ComputerName $computername -File whatever.ps1
    } else {
      Invoke-Command -CoputerName $computername -File another.ps1
    }
    

    No?

  • #19960
    Profile photo of GS
    GS
    Participant

    "The goal is to test the computer name, and based on that, run a script on that computer?"

    Yes, thanks that's the goal but I want content come from the script itsself. Can i write something in script file itself and load it within script instead. This is powershell module and if I use -File approach I need to put them on DFS and have to deal with authentication issues etc. I'd like to have those pieces to be part of module its self somehow and reuse those pieces.

    Greg

  • #19961
    Profile photo of Don Jones
    Don Jones
    Keymaster

    No, you're misunderstanding.

    When you use -File, you're not telling the remote computer, "here's a filename, go open it up and run it." That isn't what it does.

    When you use -File, [i]the computer where Invoke-Command is run[/i] will open up the file, load it, and transmit the contents to the remote computer. The file can be entirely local. You don't have to do DFS or any of that.

  • #19962
    Profile photo of GS
    GS
    Participant

    I understand the concept but it's still not accomplish the goal which is having reusable piece of code which I want to run on both computers. For example on SERVER1 I want to delete c:\test.txt and d:\test.txt and on SERVER2 I want to delete c:\test.txt and e:\test.txt. So I want the first part to be part of something reusable. Obviously real case scenario is much more complicated but I need to figure out how can I have reusable code as part of $script variable to be loaded.

  • #19966
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Sorry = we'll see if anyone else chimes in, then. I don't think I'm understanding the goal or what you mean by "reusable," here. Very sorry!

  • #19969
    Profile photo of GS
    GS
    Participant

    I want del c:\a.txt to be coming from somewhere else withing script module in example below.

    if ($computerName -match "SERVER1")
    {
    	$scriptToExecute = {
    	del c:\a.txt
    	del d:\a.txt
    	}
    }
    elseif ($computerName -match "SERVER2")
    	{
    	$scriptToExecute = {
    	del c:\a.txt
    	del e:\a.txt
    	}
    }
    
    
    

You must be logged in to reply to this topic.