?
Solved

load a file $INAME.ps1 containing variables

Posted on 2014-09-09
2
Medium Priority
?
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

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 71

Accepted Solution

by:
Chris Dent earned 1500 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

765 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