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

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]}
	}

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?

“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

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, the computer where Invoke-Command is run 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.

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.

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!

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
	}
}