Solved

load a file $INAME.ps1 containing variables

Posted on 2014-09-09
2
152 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 50

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

695 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