Solved

load a file $INAME.ps1 containing variables

Posted on 2014-09-09
2
151 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 49

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A brief introduction to what I consider to be the best editor for PowerShell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

733 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