Solved

load a file $INAME.ps1 containing variables

Posted on 2014-09-09
2
147 Views
Last Modified: 2014-09-25
Hello,

I try to load a file $INAME.ps1 containing variables. This file is in $CONF_INST_CONFIG directory which is detected automatically with the following code but it doesn't work:

The error returned is :
 The corresponding line is 'foreach ( $item in $INSTVAR) { . $item.FullName }'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'The expression after '.' in a pipeline element produced an invalid object. It must result in a command name, script block or CommandInfo object.  '.  Process Exit Code -1.  The step failed.


$Global:CONF_INST_TREE_ROOT=""
$Global:CONF_INST_CONFIG="$CONF_INST_TREE_ROOT\config"
$Global:CONF_INST_SCRIPT_LOG="$CONF_INST_TREE_ROOT\log"
$SERVERNAME = (Get-WmiObject Win32_Computersystem).Name
if ($I -contains "\") {
$INAME=$I.split("\")[1]
}
else {
$INAME="MSSQLSERVER"
}
$WORKDIR=(get-location).providerpath 
$GLOBAL=(Split-Path -parent $WORKDIR)
$GLOBALVAR = gci -Recurse ($GLOBAL) -in global.ps1 
$REGPATH=((REG QUERY "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" /v $INAME /reg:64 | Select-String -pattern $INAME) -split "\s+")[-1] 
$FULLPATH = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$REGPATH"
$SQLROOT = (Get-ItemProperty "$fullpath\Setup").SQLDataRoot
$CONF_INST_TREE_ROOT = ($SQLROOT.Split("\") | Select -First 3) -join ("\")

  if ($CONF_INST_TREE_ROOT -match $INAME) {
	$CONF_INST_CONFIG=$CONF_INST_TREE_ROOT + "\" + "config"
  } 
  else {
  Push-Location $CONF_INST_TREE_ROOT 
  $IROOT=Get-ChildItem $CONF_INST_TREE_ROOT  | ? { $_.PSIsContainer } | Where-Object { $_.name -match "$INAME"}
  $CONF_INST_CONFIG = $CONF_INST_TREE_ROOT + "\" + $IROOT + "\" + "config" 
  
  }

foreach ( $item in $GLOBALVAR) { . $item.FullName }
$INSTVAR = gci -Recurse ($CONF_INST_CONFIG) -in $INAME.ps1 

Open in new window


How can I resolve it?

Thanks
0
Comment
Question by:bibi92
2 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40314154
I don't think you need the '.' before $item.FullName .
Should be:
foreach ( $item in $INSTVAR) { $item.FullName }

Open in new window

0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 40314206
If you're trying to load the content of the file into the current session dot-sourcing is the right thing to do (or at least a right thing to do).

Perhaps echo out (Write-Host) the value you're trying to dot-source? Or use Test-Path? I suspect you're trying to feed a null value in; Looping on $null is something of a feature of PowerShell. You can get the same error if you run:
. $null

Open in new window

Chris
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now